﻿<?php
class gameAction extends TAction
{
	
	//授权初始页url：http://kanjia.loc/game/oauth_authorize?goto=http%253A%252F%252Fkanjia.loc%252Fhome%252Fcut%252Fuid%252Fabc001
	public function oauth_authorize(){
		
		if(isset($_REQUEST['state'])){
			$state=$_REQUEST['state'];
		}
		else{
			$state="STATE";
		}
		
		if(isset($_REQUEST['goto'])){
			$goto=$_REQUEST['goto'];
		}
		else{
			$goto="";
		}
		//echo $goto;exit;
		$goto=urlencode($goto);
		//echo $goto;exit;
		
		$redirect_uri=BASE_URL_WEBSITE.U('game/oauth_token')."?goto=".$goto;
		//echo $redirect_uri;exit;
		$redirect_uri=urlencode($redirect_uri);
		//echo $redirect_uri;exit;
		
		
		$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.WX_APPID.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state='.$state.'#wechat_redirect';
		//echo $url;exit;
		redirect($url);
		exit;
		
	}
	
	
	//用拿到的code，换取access_token：
	public function oauth_token(){
		
		if(isset($_REQUEST['state'])){
			$state=$_REQUEST['state'];
		}
		else{
			$state='STATE';
		}
		
		if(isset($_REQUEST['code'])){
			$code=$_REQUEST['code'];
		}
		else{
			$code='';
		}
		
		if(isset($_REQUEST['goto'])){
			$goto=$_REQUEST['goto'];
		}
		else{
			$goto="";
		}
		
		//echo $state."<br>";
		//echo $code."<br>";
		//echo $goto."<br>";
		//exit;
		
		$get_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.WX_APPID.'&secret='.WX_APPSECRET.'&code='.$code.'&grant_type=authorization_code';
		$get_return = file_get_contents($get_url);
		$get_return = (array)json_decode($get_return);
		//echo "<pre>";print_r($get_return);
		
		$_SESSION['WX_INFO']=$get_return;
		//echo "kk<pre>";print_r($_SESSION);echo "</pre>";echo "<br><br>";
		
		
		//homeAction.class.php里的这个地方也要改
		if(isset($_SESSION['WX_INFO']['access_token']) && isset($_SESSION['WX_INFO']['openid']) && $_SESSION['WX_INFO']['openid']!=""){
			
			$openid=$_SESSION['WX_INFO']['openid'];
			
			//echo "aa<pre>";print_r($_SESSION['WX_INFO']);echo "</pre>";
			
			
			//拿微信用户信息
			$get_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$_SESSION['WX_INFO']['access_token'].'&openid='.$_SESSION['WX_INFO']['openid'].'&lang=zh_CN';
			$get_return = file_get_contents($get_url);
			$userinfo = (array)json_decode($get_return);
			
			//echo "bb<pre>";print_r($userinfo);echo "</pre>";exit;
			//echo "rr<pre>";print_r($userinfo);echo "</pre>";echo "<br><br>";
			
			
			//网页授权接口调用凭证,注意：此access_token与基础支持的access_token不同
			//$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$_SESSION['WX_INFO']['access_token']."&openid=".$_SESSION['WX_INFO']['openid'];
			//echo $url;echo "<br><br>";
			//$get_return_userinfo = file_get_contents($url);
			//$get_return_userinfo = (array)json_decode($get_return_userinfo);
			//echo "uu<pre>";print_r($get_return_userinfo);echo "</pre>";echo "<br><br>";
			
			
		
		
			$CityMod = M('user');
	        $user_data = $CityMod->where(" openid='".addslashes($openid)."' " )->select();
	        if(!empty($user_data)){
	        	
	        	$user_data=$user_data[0];
	        	
	        	//更新
	        	//前台判断，如果当前openid（当前用户）数据库里没昵称，且微信api拿到的昵称不为空，就更新昵称字段， 否则其他情况前台代码逻辑里都不更新昵称字段。
	        	if($user_data['nickname']=='' && $userinfo['nickname']!=''){
	        		
					$UserMod = M('user');
			        $sql=sprintf("UPDATE %s SET nickname='".addslashes($userinfo['nickname'])."' 
			        , sex='".addslashes($userinfo['sex'])."' 
			        , province='".addslashes($userinfo['province'])."' 
			        , city='".addslashes($userinfo['city'])."' 
			        , country='".addslashes($userinfo['country'])."' 
			        , headimgurl='".addslashes($userinfo['headimgurl'])."' 
			         where openid='".addslashes($openid)."' ", $UserMod->getTableName() );
			        //echo $sql;
			        $result = $UserMod->execute($sql);
		        }
	        }
	        else{
	        	//新增
		        $UserMod = M('user');
		        $sql=sprintf("INSERT %s SET openid='".$openid."' 
		        , nickname='".addslashes($userinfo['nickname'])."' 
		        , sex='".addslashes($userinfo['sex'])."' 
		        , province='".addslashes($userinfo['province'])."' 
		        , city='".addslashes($userinfo['city'])."' 
		        , country='".addslashes($userinfo['country'])."' 
		        , headimgurl='".addslashes($userinfo['headimgurl'])."' 
		        , create_time='".time()."' 
		        ", $UserMod->getTableName() );
		        //echo $sql;
		        $result = $UserMod->execute($sql);
	        }
	        
	        
	        
	        $CityMod = M('user');
	        $user_data = $CityMod->where(" openid='".addslashes($openid)."' " )->select();
	        if(isset($user_data[0]['username']) && $user_data[0]['username']!=''){
	        }
		    else{
		    	//没注册过，需要更新fuid。注册过，则不用更新fuid
		    	if(isset($_SESSION['fuid']) && !empty($_SESSION['fuid']) ){
		    		//记录上家user_id
		    		$mobileMod = M('user');
					$sql=sprintf("UPDATE %s SET fuid='".addslashes($_SESSION['fuid'])."' 
			        where openid='".addslashes($openid)."' 
			        ", $mobileMod->getTableName() );
			        $mobileMod->execute($sql);
		    	}
	        }
	        
	        
		}
		
		//exit;
		
		if(!empty($goto)){
			//echo "gg<pre>";print_r($_SESSION);exit;
			
			$url=$goto;
		}
		else{
			//echo "hh<pre>";print_r($_SESSION);exit;
			//echo "xx<pre>";print_r($_SESSION);echo "</pre>";echo "<br><br>";exit;
			
			$url=U('home/index');
		}
		
        redirect($url);
		exit;
	}
	
	
	
	
	
	
	
	public function oauth_authorize_test(){
		
		$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.WX_APPID.'&redirect_uri=http%3A%2F%2Fcbmee.maxbund.com%2Fgame%2Foauth_token_test&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
		redirect($url);
		exit;
		
	}
	
	
	
	
	public function oauth_token_test(){
		
		if(isset($_REQUEST['code'])){
			$code=$_REQUEST['code'];
		}
		else{
			$code='';
		}
		
		$get_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.WX_APPID.'&secret='.WX_APPSECRET.'&code='.$_REQUEST['code'].'&grant_type=authorization_code';
		$get_return = file_get_contents($get_url);
		$get_return = (array)json_decode($get_return);
		//echo "<pre>";print_r($get_return);
		
		$_SESSION['WX_INFO']=$get_return;
		
		
		if(isset($_SESSION['WX_INFO']['access_token']) && isset($_SESSION['WX_INFO']['openid']) && $_SESSION['WX_INFO']['openid']!=""){
			
			$openid=$_SESSION['WX_INFO']['openid'];
			
			//echo "aa<pre>";print_r($_SESSION['WX_INFO']);echo "</pre>";
			echo $_SESSION['WX_INFO']['access_token'];echo "<br><br>";
			
			//拿微信用户信息
			$get_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$_SESSION['WX_INFO']['access_token'].'&openid='.$_SESSION['WX_INFO']['openid'].'&lang=zh_CN';
			$get_return = file_get_contents($get_url);
			$userinfo = (array)json_decode($get_return);
			
			//echo "bb<pre>";print_r($userinfo);echo "</pre>";exit;
			
			
			$CityMod = M('user');
	        $user_data = $CityMod->field('id')->where(" openid='".addslashes($openid)."' " )->select();
	        if(!empty($user_data)){
	        	//更新
				$UserMod = M('user');
		        $sql=sprintf("UPDATE %s SET nickname='".addslashes($userinfo['nickname'])."' 
		        , sex='".addslashes($userinfo['sex'])."' 
		        , province='".addslashes($userinfo['province'])."' 
		        , city='".addslashes($userinfo['city'])."' 
		        , country='".addslashes($userinfo['country'])."' 
		        , headimgurl='".addslashes($userinfo['headimgurl'])."' 
		         where openid='".addslashes($openid)."' ", $UserMod->getTableName() );
		        //echo $sql;
		        $result = $UserMod->execute($sql);
	        }
	        else{
	        	//新增
		        $UserMod = M('user');
		        $sql=sprintf("INSERT %s SET openid='".$openid."' 
		        , nickname='".addslashes($userinfo['nickname'])."' 
		        , sex='".addslashes($userinfo['sex'])."' 
		        , province='".addslashes($userinfo['province'])."' 
		        , city='".addslashes($userinfo['city'])."' 
		        , country='".addslashes($userinfo['country'])."' 
		        , headimgurl='".addslashes($userinfo['headimgurl'])."' 
		        , create_time='".time()."' 
		        ", $UserMod->getTableName() );
		        //echo $sql;
		        $result = $UserMod->execute($sql);
	        }
	        
		
		}
		
		//exit;
		
		$url='/game/uploadheadpic_weixin_test';
		$a='<br><a href="'.$url.'">'.$url.'</a><br>';
		echo $a;
		//redirect($url);
		exit;
	}
	
	
	
	
	//每隔30秒刷token：http://cbmee.maxbund.com/game/oauth_refresh_token
	public function oauth_refresh_token(){
		
		if(isset($_SESSION['WX_INFO']['refresh_token'])){
		
			//第三步：刷新access_token（如果需要）
			$get_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.WX_APPID.'&grant_type=refresh_token&refresh_token='.$_SESSION['WX_INFO']['refresh_token'].'';
			$get_return = file_get_contents($get_url);
			$get_return = (array)json_decode($get_return);
			//echo "<pre>";print_r($get_return);
		
		}
		
		$this->jsonData(0,'成功');
        exit;
        
	}
	
	
	
	
	
    //回答问题是否已满5次
    public function question_is_limit(){
    	
        
    	if(isset($_SESSION['WX_INFO']['openid']) && $_SESSION['WX_INFO']['openid']!=""){
            $openid=$_SESSION['WX_INFO']['openid'];
        }
        else{
        	$this->jsonData(1,'请先授权微信认证');
            exit;
        }
        
        
        $question_limit=5;
        
        $CityMod = M('game');
        $question_info = $CityMod->field('count(id) as num')->where(" openid='".addslashes($openid)."' and is_correct>0 " )->select();
        if(isset($question_info[0]['num']) && $question_info[0]['num']>=$question_limit){
        	$this->jsonData(1,'答题已满'.$question_limit.'次');
            exit;
        }
        
        $this->jsonData(0,'成功');
    }
	
	
	
	
	
    //回答问题
    public function question(){
    	
        
    	if(isset($_SESSION['WX_INFO']['openid']) && $_SESSION['WX_INFO']['openid']!=""){
            $openid=$_SESSION['WX_INFO']['openid'];
        }
        else{
        	$this->jsonData(1,'请先授权微信认证');
            exit;
        }
        
        
        if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }
        
        
        
        if(isset($_REQUEST['question']) && $_REQUEST['question']!=''){
            $question=$_REQUEST['question'];
        }
        else{
            $this->jsonData(1,'参数错误');
            exit;
        }
        
        if(isset($_REQUEST['answer']) && $_REQUEST['answer']!=''){
            $answer=$_REQUEST['answer'];
        }
        else{
            $this->jsonData(1,'参数错误');
            exit;
        }
        
        if(isset($_REQUEST['is_correct']) && $_REQUEST['is_correct']!=''){
            $is_correct=$_REQUEST['is_correct'];
        }
        else{
            $this->jsonData(1,'参数错误');
            exit;
        }
        
        
        
        
        $question_limit=5;
        
        $CityMod = M('game');
        $question_info = $CityMod->field('count(id) as num')->where(" openid='".addslashes($openid)."' and is_correct>0 " )->select();
        if(isset($question_info[0]['num']) && $question_info[0]['num']>=$question_limit){
        	$this->jsonData(1,'答题已满'.$question_limit.'次');
            exit;
        }
        
        
        
		$UserMod = M('game');
        $sql=sprintf("UPDATE %s SET question='".addslashes($question)."' 
        , answer='".addslashes($answer)."'
        , is_correct='".addslashes($is_correct)."'
         where id='".addslashes($game_id)."' ", $UserMod->getTableName() );
        $result = $UserMod->execute($sql);
        
        
        $this->jsonData(0,'成功');
    }
	
	
	
	
	
	
    //获得当前用户个人信息  获得当前幸福值
    public function getuserinfo(){
    	
    	if(isset($_SESSION['is_login']) && $_SESSION['is_login']=='yes'){
            $user_id=$_SESSION['userinfo']['id'];
        }
        else{
        	$this->jsonData(1,'请先登陆。');
            exit;
        }
        
        
        $CityMod = M('user');
        $info = $CityMod->field('username, realname , point_total')->where(" id='".addslashes($user_id)."' " )->select();
        $username=isset($info[0]['username'])?$info[0]['username']:"";
        $realname=isset($info[0]['realname'])?$info[0]['realname']:"";
        $point_total=isset($info[0]['point_total'])?$info[0]['point_total']:"";
        
		$data['username']=$username;
		$data['realname']=$realname;
		$data['point_total']=$point_total;
        $this->jsonData(0,'成功',$data);
    }
	
	
	
	
    //游戏1上传头像  提交
    public function game1uploadheadpic(){
    	
    	if(isset($_SESSION['is_login']) && $_SESSION['is_login']=='yes'){
            $user_id=$_SESSION['userinfo']['id'];
        }
        else{
        	$this->jsonData(1,'请先登陆。');
            exit;
        }
        
    	$game_id=1;
    	
		if(isset($_REQUEST['style']) && ($_REQUEST['style']==1 || $_REQUEST['style']==2 || $_REQUEST['style']==3) ){
			$style=$_REQUEST['style'];
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		
        if(isset($_REQUEST['is_android']) && $_REQUEST['is_android']==1){
            $is_android=$_REQUEST['is_android'];
        }
        else{
            $is_android=0;
        }
        
        
		//$path = "game_".$game_id."_style_".$style."_time_".date('ymdHis')."_".rand(10,99).".png";
		$path = "game_".$game_id."_style_".$style."_time_".date('ymdHis')."_".$user_id.".png";
		
		$path='game_1_style_1_time_150716161543_1.png';
		$dest = BASE_UPLOAD_PATH.$path;
		//echo $dest;exit;
		
		
		/*
		//base64模式接受上传数据
		$f = fopen($dest,'w');
		$img = '';
		$img = str_replace('data:image/jpeg;base64,', '', $img);
		$img = str_replace('data:image/gif;base64,', '', $img);
		$img = str_replace('data:image/png;base64,', '', $img);
		$img = str_replace(' ', '+', $img);
		$datas = base64_decode($img);
		fwrite($f,$datas);
		fclose($f);
		*/
		
		
		
		$pic_imagerotate='';
		
		if($_FILES){
			//上传方式：表单
			$input = key($_FILES);
			if(!move_uploaded_file($_FILES[$input]['tmp_name'],$dest)) {
				$this->jsonData(1,'失败');
		        exit;
			}
			else{
				
				//判断EXIF头信息模式解决旋转90度问题
				$exif = exif_read_data($dest);
				$ort = isset($exif['Orientation'])?$exif['Orientation']:"";
				if($ort==""){
					$ort = isset($exif['IFD0']['Orientation'])?$exif['IFD0']['Orientation']:"";
				}
				switch($ort){
					case 1: // nothing
						break;
        			case 2: // horizontal flip
        				break;
	                case 3: // 180 rotate left  //向左旋转180度
	                    //$image->imagerotate($upload_path . $newfilename, 180, -1);
	                    $pic_imagerotate='180';
	                    break;
	                case 4: // vertical flip
            			break;
            		case 5: // vertical flip + 90 rotate right
				        $pic_imagerotate='-90';
				        break;
	                case 6: // 90 rotate right  //向右旋转90度
	                    //$image->imagerotate($upload_path . $newfilename, -90, -1);
	                    $pic_imagerotate='-90';
	                    break;
	                case 7: // horizontal flip + 90 rotate right
				        $pic_imagerotate='-90';
				        break;
	                case 8:    // 90 rotate left  //向左旋转90度
	                    //$image->imagerotate($upload_path . $newfilename, 90, -1);
	                    $pic_imagerotate='90';
	                    break;
	            }
	            //echo "<pre>";print_r($exif);exit;
	            //var_dump($pic_imagerotate);exit;
			}
		}
		elseif(isset($GLOBALS['HTTP_RAW_POST_DATA'])){ 
			
			//上传方式：原始POST
			//$f = fopen($dest,'w');
			//fwrite($f,$GLOBALS['HTTP_RAW_POST_DATA']);
			//fclose($f);
			
			//base64模式接受上传数据
			/*
			$f = fopen($dest,'w');
			$img = $GLOBALS['HTTP_RAW_POST_DATA'];
			$img = str_replace('data:image/jpeg;base64,', '', $img);
			$img = str_replace('data:image/gif;base64,', '', $img);
			$img = str_replace('data:image/png;base64,', '', $img);
			$img = str_replace(' ', '+', $img);
			$datas = base64_decode($img);
			fwrite($f,$datas);
			fclose($f);
			*/
			
			if($is_android==1){
				$f = fopen($dest,'w');
				$img = $GLOBALS['HTTP_RAW_POST_DATA'];
				$img = str_replace('data:image/jpeg;base64,', '', $img);
				$img = str_replace('data:image/gif;base64,', '', $img);
				$img = str_replace('data:image/png;base64,', '', $img);
				$img = str_replace(' ', '+', $img);
				$datas = base64_decode($img);
				fwrite($f,$datas);
				fclose($f);
			}
			else{
				//判断EXIF头信息模式解决旋转90度问题（示例：http://stackoverflow.com/questions/7489742/php-read-exif-data-and-adjust-orientation）
				$f = fopen($dest,'w');
				fwrite($f,$GLOBALS['HTTP_RAW_POST_DATA']);
				fclose($f);
			}
			
			$exif = exif_read_data($dest);
			$ort = isset($exif['Orientation'])?$exif['Orientation']:"";
			if($ort==""){
				$ort = isset($exif['IFD0']['Orientation'])?$exif['IFD0']['Orientation']:"";
			}
			switch($ort){
				case 1: // nothing
					break;
    			case 2: // horizontal flip
    				break;
                case 3: // 180 rotate left  //向左旋转180度
                    //$image->imagerotate($upload_path . $newfilename, 180, -1);
                    $pic_imagerotate='180';
                    break;
                case 4: // vertical flip
        			break;
        		case 5: // vertical flip + 90 rotate right
			        $pic_imagerotate='-90';
			        break;
                case 6: // 90 rotate right  //向右旋转90度
                    //$image->imagerotate($upload_path . $newfilename, -90, -1);
                    $pic_imagerotate='-90';
                    break;
                case 7: // horizontal flip + 90 rotate right
			        $pic_imagerotate='-90';
			        break;
                case 8:    // 90 rotate left  //向左旋转90度
                    //$image->imagerotate($upload_path . $newfilename, 90, -1);
                    $pic_imagerotate='90';
                    break;
            }
            
			
			
		}
		else{ 
			//上传方式：客户端提交
			//$f = fopen($dest,'w');
			//fwrite($f,file_get_contents('php://input'));
			//fclose($f);
			
			//base64模式接受上传数据
			/*
			$f = fopen($dest,'w');
			$img = file_get_contents('php://input');
			$img = str_replace('data:image/jpeg;base64,', '', $img);
			$img = str_replace('data:image/gif;base64,', '', $img);
			$img = str_replace('data:image/png;base64,', '', $img);
			$img = str_replace(' ', '+', $img);
			$datas = base64_decode($img);
			fwrite($f,$datas);
			fclose($f);
			*/
			
			if($is_android==1){
				$f = fopen($dest,'w');
				$img = file_get_contents('php://input');
				$img = str_replace('data:image/jpeg;base64,', '', $img);
				$img = str_replace('data:image/gif;base64,', '', $img);
				$img = str_replace('data:image/png;base64,', '', $img);
				$img = str_replace(' ', '+', $img);
				$datas = base64_decode($img);
				fwrite($f,$datas);
				fclose($f);
			}
			else{
				//判断EXIF头信息模式解决旋转90度问题
				$f = fopen($dest,'w');
				fwrite($f,file_get_contents('php://input'));
				fclose($f);
			}
			
			$exif = exif_read_data($dest);
			$ort = isset($exif['Orientation'])?$exif['Orientation']:"";
			if($ort==""){
				$ort = isset($exif['IFD0']['Orientation'])?$exif['IFD0']['Orientation']:"";
			}
			switch($ort){
				case 1: // nothing
					break;
    			case 2: // horizontal flip
    				break;
                case 3: // 180 rotate left  //向左旋转180度
                    //$image->imagerotate($upload_path . $newfilename, 180, -1);
                    $pic_imagerotate='180';
                    break;
                case 4: // vertical flip
        			break;
        		case 5: // vertical flip + 90 rotate right
			        $pic_imagerotate='-90';
			        break;
                case 6: // 90 rotate right  //向右旋转90度
                    //$image->imagerotate($upload_path . $newfilename, -90, -1);
                    $pic_imagerotate='-90';
                    break;
                case 7: // horizontal flip + 90 rotate right
			        $pic_imagerotate='-90';
			        break;
                case 8:    // 90 rotate left  //向左旋转90度
                    //$image->imagerotate($upload_path . $newfilename, 90, -1);
                    $pic_imagerotate='90';
                    break;
            }
			
			
			
		}
		
		
		//echo $exif['Orientation'];exit;
		//echo "<pre>";print_r($exif);exit;
		$dest='D:\www\ouliwei\test\ouliwei\cms/public/web_pic/game_1_style_1_time_150716161543_1.jpg';
		
		$file_type=$this->get_file_type($dest); 
		//echo $file_type;exit;
		if ($file_type!="jpg" && $file_type!="gif" && $file_type!="png"){
			@unlink($dest);
			$this->jsonData(1,'图片上传限定jpg、gif、png类型');
		    exit;
		}
		
		
		$file_z=filesize($dest);
		$f_size_limit_byte=BASE_FILE_SIZE_LIMIT*1024*1024;
		
		if ($file_z>$f_size_limit_byte){
			@unlink($dest);
			$this->jsonData(1,'图片上传限定'.BASE_FILE_SIZE_LIMIT.'M');
		    exit;
		}
		
		
		$point=5;
		
		
		$UserMod = M('game1');
        $sql=sprintf("INSERT %s SET headpic='".addslashes($path)."' 
        , imagerotate='".addslashes($pic_imagerotate)."' 
        , style='".addslashes($style)."' 
        , user_id='".addslashes($user_id)."' 
        , create_time='".time()."' 
        ", $UserMod->getTableName() );
        //echo $sql;exit;
        $result = $UserMod->execute($sql);
        
        
        $UserMod = M('user_point');
        $sql=sprintf("INSERT %s SET point='".$point."' 
        , source='game1' 
        , user_id='".addslashes($user_id)."' 
        , create_time='".time()."' 
        ", $UserMod->getTableName() );
        //echo $sql;exit;
        $result = $UserMod->execute($sql);
        
        
        $CityMod = M('user_point');
        $point_total = $CityMod->field('sum(point) as point_total , user_id')->where(" user_id='".addslashes($user_id)."' " )->select();
        $point_total=isset($point_total[0]['point_total'])?$point_total[0]['point_total']:0;
        
        
        $UserMod = M('user');
        $sql=sprintf("UPDATE %s SET point_total='".$point_total."' 
        where id='".addslashes($user_id)."' 
        ", $UserMod->getTableName() );
        $result = $UserMod->execute($sql);
        
        
		$data['headpic']=BASE_URL."/public/web_pic/".$path;
		$data['style']=$style;
		$data['point_total']=$point_total;
        $this->jsonData(0,'成功',$data);
        
    }
    
    
    
    
    //游戏1上传头像  查看当前用户最近一次提交的头像
    public function game1getheadpic(){
    	
    	if(isset($_SESSION['is_login']) && $_SESSION['is_login']=='yes'){
            $user_id=$_SESSION['userinfo']['id'];
        }
        else{
        	$this->jsonData(1,'请先登陆。');
            exit;
        }
        
    	$game_id=2;
    	
		
        
        $CityMod = M('game1');
        $info = $CityMod->field('headpic, style , user_id')->where(" user_id='".addslashes($user_id)."' " )->order('id desc')->select();
        //echo "<pre>";print_r($info);exit;
        $headpic=isset($info[0]['headpic'])?$info[0]['headpic']:"";
        $style=isset($info[0]['style'])?$info[0]['style']:"";
        
        
		$data['headpic']=BASE_URL."/public/web_pic/".$headpic;
		$data['style']=$style;
        $this->jsonData(0,'成功',$data);
        
    }
    
    
    
    
    
    
    
    
    //游戏2幸福拼图
    public function game2picture(){
    	
    	if(isset($_SESSION['is_login']) && $_SESSION['is_login']=='yes'){
            $user_id=$_SESSION['userinfo']['id'];
        }
        else{
        	$this->jsonData(1,'请先登陆。');
            exit;
        }
        
    	$game_id=2;
    	
		if(isset($_REQUEST['is_finish']) && ($_REQUEST['is_finish']==1 || $_REQUEST['is_finish']==0 ) ){
			$is_finish=$_REQUEST['is_finish'];
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		
		$UserMod = M('game2');
        $sql=sprintf("INSERT %s SET is_finish='".addslashes($is_finish)."' 
        , user_id='".addslashes($user_id)."' 
        , create_time='".time()."' 
        ", $UserMod->getTableName() );
        //echo $sql;exit;
        $result = $UserMod->execute($sql);
        
        if($is_finish==1){
        	$point=5;
        }
        else{
        	$point=0;
        }
        
        if($is_finish==1){
	        $UserMod = M('user_point');
	        $sql=sprintf("INSERT %s SET point='".$point."' 
	        , source='game2' 
	        , user_id='".addslashes($user_id)."' 
	        , create_time='".time()."' 
	        ", $UserMod->getTableName() );
	        //echo $sql;exit;
	        $result = $UserMod->execute($sql);
        }
        
        
        $CityMod = M('user_point');
        $point_total = $CityMod->field('sum(point) as point_total , user_id')->where(" user_id='".addslashes($user_id)."' " )->select();
        $point_total=isset($point_total[0]['point_total'])?$point_total[0]['point_total']:0;
        
        
        $UserMod = M('user');
        $sql=sprintf("UPDATE %s SET point_total='".$point_total."' 
        where id='".addslashes($user_id)."' 
        ", $UserMod->getTableName() );
        $result = $UserMod->execute($sql);
        
        
		$data['point_total']=$point_total;
        $this->jsonData(0,'成功',$data);
        
    }
    
    
    
    
    
    //游戏3幸福之旅  投票
    public function game3site(){
    	
    	if(isset($_SESSION['is_login']) && $_SESSION['is_login']=='yes'){
            $user_id=$_SESSION['userinfo']['id'];
        }
        else{
        	$this->jsonData(1,'请先登陆。');
            exit;
        }
        
    	$game_id=3;
    	
		if(isset($_REQUEST['site']) && ($_REQUEST['site']==1 || $_REQUEST['site']==2 || $_REQUEST['site']==3 ) ){
			$site=$_REQUEST['site'];
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		$point=5;
		
		$UserMod = M('game3');
        $sql=sprintf("INSERT %s SET site='".addslashes($site)."' 
        , user_id='".addslashes($user_id)."' 
        , create_time='".time()."' 
        ", $UserMod->getTableName() );
        //echo $sql;exit;
        $result = $UserMod->execute($sql);
        
    	
        $UserMod = M('user_point');
        $sql=sprintf("INSERT %s SET point='".$point."' 
        , source='game3' 
        , user_id='".addslashes($user_id)."' 
        , create_time='".time()."' 
        ", $UserMod->getTableName() );
        //echo $sql;exit;
        $result = $UserMod->execute($sql);
    	
        
        
        $CityMod = M('user_point');
        $point_total = $CityMod->field('sum(point) as point_total , user_id')->where(" user_id='".addslashes($user_id)."' " )->select();
        $point_total=isset($point_total[0]['point_total'])?$point_total[0]['point_total']:0;
        
        
        $UserMod = M('user');
        $sql=sprintf("UPDATE %s SET point_total='".$point_total."' 
        where id='".addslashes($user_id)."' 
        ", $UserMod->getTableName() );
        $result = $UserMod->execute($sql);
        
        
        $UserMod = M('game3_site_ticket');
        $sql=sprintf("UPDATE %s SET site".$site."_ticket=site".$site."_ticket+1  
        ", $UserMod->getTableName() );
        $result = $UserMod->execute($sql);
        
        
        $CityMod = M('game3_site_ticket');
        $rst = $CityMod->field('site1_ticket,site2_ticket,site3_ticket')->where(" id='1' " )->select();
        $site1_ticket=isset($rst[0]['site1_ticket'])?$rst[0]['site1_ticket']:0;
        $site2_ticket=isset($rst[0]['site2_ticket'])?$rst[0]['site2_ticket']:0;
        $site3_ticket=isset($rst[0]['site3_ticket'])?$rst[0]['site3_ticket']:0;
        
        
        $data['site1_ticket']=$site1_ticket;
        $data['site2_ticket']=$site2_ticket;
        $data['site3_ticket']=$site3_ticket;
		$data['point_total']=$point_total;
        $this->jsonData(0,'成功',$data);
        
    }
    
    
    
    
    //游戏3幸福之旅  当前票数情况
    public function game3siteticket(){
    	
        
        $CityMod = M('game3_site_ticket');
        $rst = $CityMod->field('site1_ticket,site2_ticket,site3_ticket')->where(" id='1' " )->select();
        $site1_ticket=isset($rst[0]['site1_ticket'])?$rst[0]['site1_ticket']:0;
        $site2_ticket=isset($rst[0]['site2_ticket'])?$rst[0]['site2_ticket']:0;
        $site3_ticket=isset($rst[0]['site3_ticket'])?$rst[0]['site3_ticket']:0;
        
        
        $data['site1_ticket']=$site1_ticket;
        $data['site2_ticket']=$site2_ticket;
        $data['site3_ticket']=$site3_ticket;
        $this->jsonData(0,'成功',$data);
        
    }
    
    
    
    
    //是否允许兑换15分幸福值活动
    public function prize_allow_15( $thisfunc=false ){
    
    	if(isset($_SESSION['is_login']) && $_SESSION['is_login']=='yes'){
            $user_id=$_SESSION['userinfo']['id'];
        }
        else{
        	$this->jsonData(1,'请先登陆。');
            exit;
        }
        
        $prize_type=15;
        
        
        $CityMod = M('prize_history');
        $rst = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and bag_address!='' " )->select();
        
        if(!empty($rst)){
          	$this->jsonData(1,'您已经参加过'.$prize_type.'分幸福值活动');
        }
        
        
        $CityMod = M('user');
        $userinfo = $CityMod->field('id,point_total')->where(" id='".$user_id."' " )->select();
        //echo "<pre>";print_r($userinfo);exit;
        $point_total=isset($userinfo[0]['point_total'])?$userinfo[0]['point_total']:0;
        //echo $point_total;exit;
        
        if($point_total<$prize_type){
        	$this->jsonData(1,'您的幸福值不够');
        }
        
        if($thisfunc==false){
        	$this->jsonData(0,'成功');
        }
        if($thisfunc==true){
        	return true;
        }
        
    }
    
    
    
    
    
    //是否允许兑换20分幸福值活动(抽奖)
    public function prize_allow_20( $thisfunc=false ){
    
    	if(isset($_SESSION['is_login']) && $_SESSION['is_login']=='yes'){
            $user_id=$_SESSION['userinfo']['id'];
        }
        else{
        	$this->jsonData(1,'请先登陆。');
            exit;
        }
        
        $prize_type=20;
        
        
        $CityMod = M('prize_history');
        $rst = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and bag_address!='' " )->select();
        
        if(!empty($rst)){
          	$this->jsonData(1,'您已经参加过'.$prize_type.'分幸福值活动');
        }
        
        
        $CityMod = M('prize_history');
        $rst = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and iphone_is_prize!='0'  " )->select();
        
        if(!empty($rst)){
          	$this->jsonData(1,'您已经参加过'.$prize_type.'分幸福值活动。');
        }
        
        
        $now_date=date('Y-m-d');
        
        $CityMod = M('prize_history');
        $rst = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and bag_address!='' and now_date='".$now_date."' " )->select();
        
        if(!empty($rst)){
          	$this->jsonData(1,'您今天已经参加过'.$prize_type.'分幸福值活动');
        }
        
        
        $CityMod = M('prize_history');
        $rst = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and iphone_is_prize!='0' and now_date='".$now_date."'  " )->select();
        
        if(!empty($rst)){
          	$this->jsonData(1,'您今天已经参加过'.$prize_type.'分幸福值活动。');
        }
        
        
        $CityMod = M('user');
        $userinfo = $CityMod->field('id,point_total')->where(" id='".$user_id."' " )->select();
        //echo "<pre>";print_r($userinfo);exit;
        $point_total=isset($userinfo[0]['point_total'])?$userinfo[0]['point_total']:0;
        //echo $point_total;exit;
        
        if($point_total<$prize_type){
        	$this->jsonData(1,'您的幸福值不够');
        }
        
        
        if($thisfunc==false){
        	$this->jsonData(0,'成功');
        }
        if($thisfunc==true){
        	return true;
        }
        
    }
    
    
    
    
    	
    	
    
    
    //提交问卷
    public function prize_survey(){
    	
        
    	if(isset($_SESSION['is_login']) && $_SESSION['is_login']=='yes'){
            $user_id=$_SESSION['userinfo']['id'];
        }
        else{
        	$this->jsonData(1,'请先登陆。');
            exit;
        }
		
        
    	
		if(isset($_REQUEST['prize_type']) && ($_REQUEST['prize_type']==15 || $_REQUEST['prize_type']==20 ) ){
			$prize_type=$_REQUEST['prize_type'];
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		if(isset($_REQUEST['survey_address']) && $_REQUEST['survey_address']!="" ){
			$survey_address=$_REQUEST['survey_address'];
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		
		if(isset($_REQUEST['survey_mobile']) && $_REQUEST['survey_mobile']!="" ){
			$survey_mobile=$_REQUEST['survey_mobile'];
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		
		if($_SESSION['userinfo']['username']!=$survey_mobile){
			$this->jsonData(1,'您提交的手机与您当前登陆的手机号不符，请使用您当前登陆的手机号参与活动。');
            exit;
		}
		
		
		if($prize_type==15){
			$resp=$this->prize_allow_15( true );
		}
		
		if($prize_type==20){
			$resp=$this->prize_allow_20( true );
		}
		
		//var_dump($resp);exit;
		
		if($resp==true){
			//可以继续
		}
		
		
		
		$now_date=date('Y-m-d');
		$survey_1=isset($_REQUEST['survey_1'])?$_REQUEST['survey_1']:'';
		$survey_2=isset($_REQUEST['survey_2'])?$_REQUEST['survey_2']:'';
		$survey_3=isset($_REQUEST['survey_3'])?$_REQUEST['survey_3']:'';
		$survey_4=isset($_REQUEST['survey_4'])?$_REQUEST['survey_4']:'';
		$survey_5=isset($_REQUEST['survey_5'])?$_REQUEST['survey_5']:'';
		$survey_6=isset($_REQUEST['survey_6'])?$_REQUEST['survey_6']:'';
		$survey_7=isset($_REQUEST['survey_7'])?$_REQUEST['survey_7']:'';
		$survey_8=isset($_REQUEST['survey_8'])?$_REQUEST['survey_8']:'';
		$survey_9=isset($_REQUEST['survey_9'])?$_REQUEST['survey_9']:'';
		$survey_10=isset($_REQUEST['survey_10'])?$_REQUEST['survey_10']:'';
		$survey_address=isset($_REQUEST['survey_address'])?$_REQUEST['survey_address']:'';
		$survey_mobile=isset($_REQUEST['survey_mobile'])?$_REQUEST['survey_mobile']:'';
		
		
        $CityMod = M('prize_history');
        $rst = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and now_date='".$now_date."' " )->select();
        if(isset($rst[0])){
        	//更新
        	$prize_history_id=$rst[0]['id'];
        	
        	$UserMod = M('prize_history');
	        $sql=sprintf("UPDATE %s SET 
	         survey_1='".$survey_1."' 
	        , survey_2='".$survey_2."' 
	        , survey_3='".$survey_3."' 
	        , survey_4='".$survey_4."' 
	        , survey_5='".$survey_5."' 
	        , survey_6='".$survey_6."' 
	        , survey_7='".$survey_7."' 
	        , survey_8='".$survey_8."' 
	        , survey_9='".$survey_9."' 
	        , survey_10='".$survey_10."' 
	        , survey_address='".$survey_address."' 
	        , survey_mobile='".$survey_mobile."' 
	        where id=".$prize_history_id." 
	        ", $UserMod->getTableName() );
	        //echo $sql;exit;
	        $result = $UserMod->execute($sql);
	        
        }
        else{
        	//新增
        	
	        $UserMod = M('prize_history');
	        $sql=sprintf("INSERT %s SET user_id='".$user_id."' 
	        , prize_type='".$prize_type."' 
	        , now_date='".$now_date."' 
	        , survey_1='".$survey_1."' 
	        , survey_2='".$survey_2."' 
	        , survey_3='".$survey_3."' 
	        , survey_4='".$survey_4."' 
	        , survey_5='".$survey_5."' 
	        , survey_6='".$survey_6."' 
	        , survey_7='".$survey_7."' 
	        , survey_8='".$survey_8."' 
	        , survey_9='".$survey_9."' 
	        , survey_10='".$survey_10."' 
	        , survey_address='".$survey_address."' 
	        , survey_mobile='".$survey_mobile."' 
	        , create_time='".time()."' 
	        ", $UserMod->getTableName() );
	        //echo $sql;exit;
	        $result = $UserMod->execute($sql);
	        
        }
        
        $this->jsonData(0,'成功');
        
    }
    
    
    
    
    
    
    //幸福礼包领取（15分活动的情况 或 20分活动没中奖的情况）
    public function prize_bag(){
    	
        
    	if(isset($_SESSION['is_login']) && $_SESSION['is_login']=='yes'){
            $user_id=$_SESSION['userinfo']['id'];
        }
        else{
        	$this->jsonData(1,'请先登陆。');
            exit;
        }
        
    	
		if(isset($_REQUEST['prize_type']) && ($_REQUEST['prize_type']==15 || $_REQUEST['prize_type']==20 ) ){
			$prize_type=$_REQUEST['prize_type'];
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		if($prize_type==15){
			$resp=$this->prize_allow_15( true );
		}
		
		if($prize_type==20){
			$resp=$this->prize_allow_20( true );
		}
		
		//var_dump($resp);exit;
		
		if($resp==true){
			//可以继续
		}
		
		
		
		
		if($prize_type==15){
			//扣除幸福值
	        $point='-15';
	        $UserMod = M('user_point');
	        $sql=sprintf("INSERT %s SET point='".$point."' 
	        , source='prize15' 
	        , user_id='".addslashes($user_id)."' 
	        , create_time='".time()."' 
	        ", $UserMod->getTableName() );
	        //echo $sql;exit;
	        $result = $UserMod->execute($sql);
	        
	        
	        $CityMod = M('user_point');
	        $point_total = $CityMod->field('sum(point) as point_total , user_id')->where(" user_id='".addslashes($user_id)."' " )->select();
	        $point_total=isset($point_total[0]['point_total'])?$point_total[0]['point_total']:0;
	        
	        
	        $UserMod = M('user');
	        $sql=sprintf("UPDATE %s SET point_total='".$point_total."' 
	        where id='".addslashes($user_id)."' 
	        ", $UserMod->getTableName() );
	        $result = $UserMod->execute($sql);
	        //扣除幸福值
        }
        
		
		
		$now_date=date('Y-m-d');
		$bag_area=isset($_REQUEST['bag_area'])?$_REQUEST['bag_area']:'';
		$bag_address=isset($_REQUEST['bag_address'])?$_REQUEST['bag_address']:'';
		
		
        $CityMod = M('prize_history');
        $rst = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and now_date='".$now_date."' " )->select();
        if(isset($rst[0])){
        	//更新
        	$prize_history_id=$rst[0]['id'];
        	
        	$UserMod = M('prize_history');
	        $sql=sprintf("UPDATE %s SET 
	         bag_area='".$bag_area."' 
	        , bag_address='".$bag_address."' 
	        where id=".$prize_history_id." 
	        ", $UserMod->getTableName() );
	        //echo $sql;exit;
	        $result = $UserMod->execute($sql);
	        
        }
        else{
        	//新增
        	
	        $UserMod = M('prize_history');
	        $sql=sprintf("INSERT %s SET user_id='".$user_id."' 
	        , prize_type='".$prize_type."' 
	        , now_date='".$now_date."' 
	        , bag_area='".$bag_area."' 
	        , bag_address='".$bag_address."' 
	        , create_time='".time()."' 
	        ", $UserMod->getTableName() );
	        //echo $sql;exit;
	        $result = $UserMod->execute($sql);
	        
        }
        
        $this->jsonData(0,'成功');
        
    }
    
    
    
    
    //请求抽iphone奖
    public function prize_iphone(){
    	
        
    	if(isset($_SESSION['is_login']) && $_SESSION['is_login']=='yes'){
            $user_id=$_SESSION['userinfo']['id'];
        }
        else{
        	$this->jsonData(1,'请先登陆。');
            exit;
        }
        
    	
		$prize_type=20;
		
		
		if($prize_type==15){
			$resp=$this->prize_allow_15( true );
		}
		
		if($prize_type==20){
			$resp=$this->prize_allow_20( true );
		}
		
		//var_dump($resp);exit;
		
		if($resp==true){
			//可以继续
		}
		
		
		
		$rand_num=rand(0, 10000);
		$is_prize_key=9;   //rand_num等于is_prize_key的时候，为中奖。
		
		if($rand_num==$is_prize_key){
			$iphone_is_prize="1";
		}
		else{
			$iphone_is_prize="2";
		}
		
		//echo $rand_num;exit;
		
		$now_date=date('Y-m-d');
		
		
		
		$CityMod = M('prize_history');
        $sury = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and now_date='".$now_date."' and survey_mobile!='' " )->select();
        if(empty($sury)){
        	$this->jsonData(1,'请先完成问卷调查');
        }
		
		
		$CityMod = M('prize_history');
        $sury = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and iphone_is_prize=1 " )->select();
        if(!empty($sury)){
        	$iphone_is_prize="2";
        	//$this->jsonData(1,'您已经中过奖了');
        }
        
        
        
        
        
        
        //扣除幸福值
        $point='-20';
        $UserMod = M('user_point');
        $sql=sprintf("INSERT %s SET point='".$point."' 
        , source='prize20' 
        , user_id='".addslashes($user_id)."' 
        , create_time='".time()."' 
        ", $UserMod->getTableName() );
        //echo $sql;exit;
        $result = $UserMod->execute($sql);
        
        
        $CityMod = M('user_point');
        $point_total = $CityMod->field('sum(point) as point_total , user_id')->where(" user_id='".addslashes($user_id)."' " )->select();
        $point_total=isset($point_total[0]['point_total'])?$point_total[0]['point_total']:0;
        
        
        $UserMod = M('user');
        $sql=sprintf("UPDATE %s SET point_total='".$point_total."' 
        where id='".addslashes($user_id)."' 
        ", $UserMod->getTableName() );
        $result = $UserMod->execute($sql);
        //扣除幸福值
        
        
        
        
		
        $CityMod = M('prize_history');
        $rst = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and now_date='".$now_date."' " )->select();
        if(isset($rst[0])){
        	//更新
        	$prize_history_id=$rst[0]['id'];
        	
        	$UserMod = M('prize_history');
	        $sql=sprintf("UPDATE %s SET 
	         iphone_is_prize='".$iphone_is_prize."' 
	        where id=".$prize_history_id." 
	        ", $UserMod->getTableName() );
	        //echo $sql;exit;
	        $result = $UserMod->execute($sql);
	        
        }
        else{
        	//新增
        	
	        $UserMod = M('prize_history');
	        $sql=sprintf("INSERT %s SET user_id='".$user_id."' 
	        , prize_type='".$prize_type."' 
	        , now_date='".$now_date."' 
	        , iphone_is_prize='".$iphone_is_prize."' 
	        , create_time='".time()."' 
	        ", $UserMod->getTableName() );
	        //echo $sql;exit;
	        $result = $UserMod->execute($sql);
	        
        }
        
        $data['iphone_is_prize']=$iphone_is_prize;
        $this->jsonData(0,'成功',$data);
        
    }
    
    
    
    
    
    
    //抽iphone中奖后提交个人信息
    public function prize_iphone_userinfo(){
    	
        
    	if(isset($_SESSION['is_login']) && $_SESSION['is_login']=='yes'){
            $user_id=$_SESSION['userinfo']['id'];
        }
        else{
        	$this->jsonData(1,'请先登陆。');
            exit;
        }
        
    	
    	
		if(isset($_REQUEST['iphone_realname']) && $_REQUEST['iphone_realname']!="" ){
			$iphone_realname=$_REQUEST['iphone_realname'];
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		
		if(isset($_REQUEST['iphone_idcard']) && $_REQUEST['iphone_idcard']!="" ){
			$iphone_idcard=$_REQUEST['iphone_idcard'];
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		
		if(isset($_REQUEST['iphone_mobile']) && $_REQUEST['iphone_mobile']!="" ){
			$iphone_mobile=$_REQUEST['iphone_mobile'];
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		
		if(isset($_REQUEST['iphone_address']) && $_REQUEST['iphone_address']!="" ){
			$iphone_address=$_REQUEST['iphone_address'];
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		
		
		
		$prize_type=20;
		
		
		//if($prize_type==15){
		//	$resp=$this->prize_allow_15( true );
		//}
		
		//if($prize_type==20){
		//	$resp=$this->prize_allow_20( true );
		//}
		
		//var_dump($resp);exit;
		
		if($resp==true){
			//可以继续
		}
		
		
		
		
		$now_date=date('Y-m-d');
		
		
		
        
        
		
		
		$CityMod = M('prize_history');
        $sury = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and now_date='".$now_date."' and survey_mobile!='' " )->select();
        if(empty($sury)){
        	$this->jsonData(1,'请先完成问卷调查');
        }
		
		
		$CityMod = M('prize_history');
        $sury = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and now_date='".$now_date."' and iphone_is_prize=1 " )->select();
        if(empty($sury)){
        	$this->jsonData(1,'您需要先中奖，之后才能填写个人信息');
        }
		
		
		$CityMod = M('prize_history');
        $sury = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and now_date='".$now_date."' and iphone_mobile!=''  " )->select();
        if(!empty($sury)){
        	$this->jsonData(1,'您已经填写过个人信息');
        }
		
		
		
		if($_SESSION['userinfo']['username']!=$iphone_mobile){
			$this->jsonData(1,'您提交的手机与您当前登陆的手机号不符，请使用您当前登陆的手机号参与活动。');
            exit;
		}
		
		
		
		
        $CityMod = M('prize_history');
        $rst = $CityMod->field('id')->where(" user_id='".$user_id."' and prize_type='".$prize_type."' and now_date='".$now_date."' and iphone_is_prize=1 " )->select();
        if(isset($rst[0])){
        	//更新
        	$prize_history_id=$rst[0]['id'];
        	
        	$UserMod = M('prize_history');
	        $sql=sprintf("UPDATE %s SET 
	         iphone_realname='".$iphone_realname."' 
	        ,iphone_idcard='".$iphone_idcard."' 
	        ,iphone_mobile='".$iphone_mobile."' 
	        ,iphone_address='".$iphone_address."' 
	        where id=".$prize_history_id." 
	        ", $UserMod->getTableName() );
	        //echo $sql;exit;
	        $result = $UserMod->execute($sql);
	        
        }
        else{
        	$this->jsonData(1,'失败');
        }
        
        
        $this->jsonData(0,'成功');
        
    }
    
    
    
    //end
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
	//日志。
    public function logs(){
    	
        if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }
        
        
        if(isset($_REQUEST['btn']) && $_REQUEST['btn']>0 ){
			$btn=$_REQUEST['btn'];
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		
        $LogMod = M('log');
        $LogMod->btn=$btn;
        $LogMod->game_id=$game_id;
        $LogMod->addtime=date("Y-m-d H:i:s",time());
        $LogMod->add();
        
        $data['game_id']=$game_id;
        $this->jsonData(0,'成功',$data);
        exit;
    }
    
    
	
    //登记用户进入活动，点击开始参与活动。
    public function userstart(){
    	
    	
    	if(isset($_SESSION['WX_INFO']['openid']) && $_SESSION['WX_INFO']['openid']!=""){
            $openid=$_SESSION['WX_INFO']['openid'];
        }
        else{
        	$openid='';
        }
        
        
        
        if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
        	$game_id=$_SESSION['game_id'];
            $data['game_id']=$game_id;
            
            $this->jsonData(0,'成功',$data);
            exit;
        }
        else{
        	$cli_os=$this->get_client_os();
            $cur_time=time();
            
            $UserMod = M('game');
            $UserMod->openid=$openid;
			$UserMod->cli_os=$cli_os;
            $UserMod->modify_time=$cur_time;
            $UserMod->create_time=$cur_time;
            $UserMod->addtime=date("Y-m-d H:i:s",$cur_time);

            $game_id = $UserMod->add();

            if($game_id>0){
                $data['game_id']=$game_id;
                $_SESSION['game_id']=$game_id;
                
                $this->jsonData(0,'成功',$data);
                exit;
            }
            else{
                $this->jsonData(1,'失败');
                exit;
            }
        }
    }
    
    
    //点击重新参与活动
    public function restart(){
    	$this->clear_game();
        $this->userstart();
    }
    


    //验证兑换码是否正确
    public function checkcode($code=""){
        $para['no']=$code;
        $para['sign']=md5('game_2014_beta2'.$code);
        //echo "<pre>";print_r($para);exit;
        $req_url='http://g2.reloadbuzz.com/crunchywafer/checkCode';
        $result=$this->http_request_url_post($req_url,$para);
        //echo "<pre>";print_r($result);exit;
        //var_dump(json_decode($result));exit;
        //$rst=str_replace('callback(','',$result);
        //$rst=str_replace(');','',$rst);
        //$rst_arr=json_decode($rst,true);
        //$rst_arr['code']=0;  //强制返回验证成功
        if(isset($result['code']) && $result['code']==0){
        	return true;
        }
        
        return false;
    }



    //兑换码是否被使用过
    public function isused($code=""){
    	//不判断是否重复使用
    	return false;
    	
        $CityMod = M('game');
        $isused_code = $CityMod->field('id,code')->where(" code='".addslashes($code)."' and code_isused=1 " )->order('id desc')->select();
        if($isused_code==false){
            return false;
        }
        else{
            return true;
        }
    }


    //使用兑换码
    public function usecode(){
		$code=$_REQUEST['code'];
		
        if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }

        $checkcode=$this->checkcode($code);
        //var_dump($checkcode);exit;
        if($checkcode==false){
            $data['game_id']=$game_id;
            $data['code']=$code;
            $this->jsonData(1,'兑换码验证失败',$data);
            exit;
        }
		
        $isusedcode=$this->isused($code);
        if($isusedcode==true){
            $data['game_id']=$game_id;
            $data['code']=$code;
            $this->jsonData(1,'该兑换码已被使用过',$data);
            exit;
        }
		
        $UserMod = M('game');
        $sql=sprintf("UPDATE %s SET code_isused='1',code='".addslashes($code)."' where id='".addslashes($game_id)."' ", $UserMod->getTableName() );
        $result = $UserMod->execute($sql);
        
        $data['game_id']=$game_id;
        $data['code']=$code;
        $this->jsonData(0,'成功',$data);
        exit;
        

        if($result==1){
            $data['game_id']=$game_id;
            $data['code']=$code;
            $this->jsonData(0,'成功',$data);
            exit;
        }
        else{
            $data['game_id']=$game_id;
            $data['code']=$code;
            $this->jsonData(1,'失败',$data);
            exit;
        }

    }
	
	
	//获得当前用户所有信息
	public function userinfo(){
		
		if(isset($_REQUEST['game_id']) && $_REQUEST['game_id']>0 ){
			$game_id=$_REQUEST['game_id'];
		}
		else{
			if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
			    $game_id=$_SESSION['game_id'];
			}
			else{
			    $this->jsonData(1,'请先参与游戏');
			    exit;
			}
		}
		
		
		
		
		$CityMod = M('game');
        $userinfo = $CityMod->field('id as game_id
        ,headpic
        ,headegg
        ,gamefilter
        ,isfenxiang
        
        ')->where(" id='".$game_id."' " )->order('id desc')->limit('0,1')->select();
        if(isset($userinfo[0])){
        	$userinfo=$userinfo[0];
        	$userinfo['headpic']=empty($userinfo['headpic'])?"":BASE_URL."/public/web_pic/".$userinfo['headpic'];
        	$userinfo['headegg']=empty($userinfo['headegg'])?"":BASE_URL."/public/web_resize/".$userinfo['headegg'];
        	$userinfo['gamefilter']=empty($userinfo['gamefilter'])?"":BASE_URL."/public/web_filter/".$userinfo['gamefilter'];
        	
        	//echo "<pre>";print_r($userinfo);exit;
        	
        	$data=$userinfo;
        	
            $this->jsonData(0,'成功',$data);
            exit;
        }
        else{
        	$data['game_id']=$game_id;
            $data['code']=$code;
            $data=array();
            $this->jsonData(1,'失败',$data);
            exit;
        }
	}

	 
	 

	 //叠加相框合成最终效果图
	 public function gamefinish(){
	 	
	 	
	 	if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }
        
        
		
		$CityMod = M('game');
		$userinfo = $CityMod->field('id,headegg')->where(" id='".addslashes($game_id)."' " )->order('id desc')->limit('0,1')->select();
        //echo "<pre>";print_r($userinfo[0]);exit;
        if(!isset($userinfo[0])){
        	$this->jsonData(1,'请先参与游戏');
            exit;
        }
        $userinfo=$userinfo[0];
        
		
		$org_path_1=empty($userinfo['headegg'])?"":BASE_PIC_RESIZE_PATH.$userinfo['headegg'];
		
		//调试
		//$org_path_1='D:/www/nac/public/web_resize/1812_resize_150719190818_24.png';   //本地环境
		//$org_path_1='D:/www/ford/ccsfriend/cbmee/public/web_resize/1812_resize_150719190818_24.png';  //服务器环境
		
		$background_path=BASE_PIC_BACKGROUND_PATH.'game_background.png';
		
		
		$path=$game_id."_filter_".date('ymdHis')."_".rand(10,99).".png";
		$out_path=BASE_PIC_FILTER_PATH.$path;
		
		/*
		裁切的图：346*385
		相框底图：545*553
		宽相减除以2，高相减除以2。
		距离左边99.5   距离上边84
		带阴影：95   71
		不带阴影：45   47
		*/
		
		$dst_x_1=45; //父亲坐标，以左上角为基点
		$dst_y_1=47; //父亲坐标，以左上角为基点
		$dst_x_2=""; //母亲坐标，以左上角为基点
		$dst_y_2=""; //母亲坐标，以左上角为基点
		$dst_x_3=""; //小孩坐标，以左上角为基点
		$dst_y_3=""; //小孩坐标，以左上角为基点
		
		
		$percent="";
		$percent_show="";
		$score_x="";
		$score_y="";
		$percent_x="";
		$percent_y="";
		
		
		$filter_rst=$this->combine_pic_single($background_path,$out_path,$org_path_1,$dst_x_1,$dst_y_1,$score,$percent_show,$score_x,$score_y,$percent_x,$percent_y);
		
		
		if($filter_rst==true){
	        $UserMod = M('game');
	        $sql=sprintf("UPDATE %s SET gamefilter='".$path."'  where id='".addslashes($game_id)."' ", $UserMod->getTableName() );
	        $result = $UserMod->execute($sql);
	        
	        $data['game_id']=$game_id;
	        $data['pic_url']=BASE_URL."/public/web_filter/".$path;
	        $this->jsonData(0,'成功',$data);
        }
        else{
        	$this->jsonData(1,'图片合成失败');
            exit;
        }
        exit;
	 }
	 
	 
	 
	 
	 
	 
	 
	 
	 //图片合成（单张）
	 public function combine_pic($org_path,$background_path,$out_path,$dst_x,$dst_y){
	 	
		//$org_path ='D:/www/cuicuisha/gd_test/out.jpg'; //原始图片（此处原始图，即之前裁切后的图片）
		//$background_path = "D:/www/cuicuisha/gd_test/back.png";  //底图
		//$out_path = 'D:/www/cuicuisha/gd_test/gamepic.jpg';  //目标图片

		//$dst_x=550;  //原始图坐落到背景画布的坐标x（根据需求写死），以左上角为基点
		//$dst_y=250;  //原始图坐落到背景画布的坐标y（根据需求写死），以左上角为基点
		
		//叠加图片及图片合成

		$img_background_info=getimagesize($background_path);
		//echo "<pre>";print_r($img_background_info);exit;
		$bg_width=$img_background_info[0];
		$bg_height=$img_background_info[1];


		$img_org_info=getimagesize($org_path);
		//echo "<pre>";print_r($img_org_info);exit;
		$org_width=$img_org_info[0];
		$org_height=$img_org_info[1];



		$image_dst = imagecreatetruecolor($bg_width, $bg_height);  //创建一个新的目标图像

		//imagealphablending($image_dst,false);  //合并图像的时候，这句千万不能加上，否则合并出来的上层图片会遮挡下层图片。
		imagesavealpha($image_dst,true);  //这里很重要,意思是不要丢了$thumb图像的透明色;



		$white=imagecolorallocate($image_dst,255,255,255); 
		$black=imagecolorallocate($image_dst,0,0,0); 
		$red=imagecolorallocate($image_dst,255,0,0); 
		imagefill($image_dst,0,0,$white); 

		imagejpeg($image_dst, $out_path, 100);


		$img_org = @imagecreatefromjpeg($org_path);  //从原始图获取所有内容
		imagecopy($image_dst,$img_org,$dst_x,$dst_y,0,0,$org_width,$org_height);  //将原始图叠加到背景画布上



		$img_bg = @imagecreatefrompng($background_path);  //从原始图获取所有内容
		imagecopy($image_dst,$img_bg,0,0,0,0,$bg_width,$bg_height);  //将原始图叠加到背景画布上


		imagejpeg($image_dst, $out_path, 100);
	 	return true;
	 }
	 
	 
	 
	 
	 //图片合成（multi多张）
	 public function combine_pic_multi($background_path,$out_path,$org_path_1="",$dst_x_1="",$dst_y_1="",$org_path_2="",$dst_x_2="",$dst_y_2="",$org_path_3="",$dst_x_3="",$dst_y_3="",$score="",$percent="",$score_x="",$score_y="",$percent_x="",$percent_y=""){
	 	
		//$org_path ='D:/www/cuicuisha/gd_test/out.jpg'; //原始图片（此处原始图，即之前裁切后的图片）
		//$background_path = "D:/www/cuicuisha/gd_test/back.png";  //底图
		//$out_path = 'D:/www/cuicuisha/gd_test/gamepic.jpg';  //目标图片

		//$dst_x=550;  //原始图坐落到背景画布的坐标x（根据需求写死），以左上角为基点
		//$dst_y=250;  //原始图坐落到背景画布的坐标y（根据需求写死），以左上角为基点
		
		//叠加图片及图片合成

		$img_background_info=getimagesize($background_path);
		//echo "<pre>";print_r($img_background_info);exit;
		$bg_width=$img_background_info[0];
		$bg_height=$img_background_info[1];


		$img_org_info=getimagesize($org_path_1);
		$org_width_1=$img_org_info[0];
		$org_height_1=$img_org_info[1];

		$img_org_info=getimagesize($org_path_2);
		$org_width_2=$img_org_info[0];
		$org_height_2=$img_org_info[1];
		
		
		$img_org_info=getimagesize($org_path_3);
		$org_width_3=$img_org_info[0];
		$org_height_3=$img_org_info[1];
		
		

		$image_dst = imagecreatetruecolor($bg_width, $bg_height);  //创建一个新的目标图像

		//imagealphablending($image_dst,false);  //合并图像的时候，这句千万不能加上，否则合并出来的上层图片会遮挡下层图片。
		imagesavealpha($image_dst,true);  //这里很重要,意思是不要丢了$thumb图像的透明色;

		$white=imagecolorallocate($image_dst,255,255,255); 
		$black=imagecolorallocate($image_dst,0,0,0); 
		$red=imagecolorallocate($image_dst,255,0,0); 
		imagefill($image_dst,0,0,$white); 

		imagejpeg($image_dst, $out_path, 100);
		
		if($org_path_1!="" && $dst_x_1!="" && $dst_y_1!=""){
			//合并第1张原始图
			//echo $dst_x_1;echo "<br>";
			//echo $dst_y_1;echo "<br>";
			//echo $org_width_1;echo "<br>";
			//echo $org_height_1;echo "<br>";echo "<br>";
			$img_org = @imagecreatefromjpeg($org_path_1);  //从原始图获取所有内容
			imagecopy($image_dst,$img_org,$dst_x_1,$dst_y_1,0,0,$org_width_1,$org_height_1);  //将原始图叠加到背景画布上
		}
		
		if($org_path_2!="" && $dst_x_2!="" && $dst_y_2!=""){
			//合并第2张原始图
			//echo $dst_x_2;echo "<br>";
			//echo $dst_y_2;echo "<br>";
			//echo $org_width_2;echo "<br>";
			//echo $org_height_2;echo "<br>";echo "<br>";
			$img_org = @imagecreatefromjpeg($org_path_2);  //从原始图获取所有内容
			imagecopy($image_dst,$img_org,$dst_x_2,$dst_y_2,0,0,$org_width_2,$org_height_2);  //将原始图叠加到背景画布上
		}
		
		if($org_path_3!="" && $dst_x_3!="" && $dst_y_3!=""){
			//合并第3张原始图
			//echo $dst_x_3;echo "<br>";
			//echo $dst_y_3;echo "<br>";
			//echo $org_width_3;echo "<br>";
			//echo $org_height_3;echo "<br>";echo "<br>";
			$img_org = @imagecreatefromjpeg($org_path_3);  //从原始图获取所有内容
			imagecopy($image_dst,$img_org,$dst_x_3,$dst_y_3,0,0,$org_width_3,$org_height_3);  //将原始图叠加到背景画布上
		}
		

		$img_bg = @imagecreatefrompng($background_path);  //从原始图获取所有内容
		imagecopy($image_dst,$img_bg,0,0,0,0,$bg_width,$bg_height);  //将原始图叠加到背景画布上
		
		
		//无法指定文字大小和字体的做法：
		//$textcolor = imagecolorallocate($im, 0, 0, 255);  //水印文字
		//imagestring($image_dst, 4, 100, 100, "286", $textcolor);
		//$textcolor = imagecolorallocate($im, 0, 0, 255);  //水印文字
		//imagestring($image_dst, 4, 200, 200, "10%", $textcolor);
		
		
		if($score!=""){
			//可指定文字大小和字体的做法：
			$black = imagecolorallocate($im, 0, 0, 0);
			$font = ROOT_PATH.'/statics/font/arial.ttf';
			//image 图像资源 , size 字体大小 , angle 角度 , x , y , color , fontfile , text 
			$text = $score;
			imagettftext($image_dst, 16, 0, $score_x, $score_y, $black, $font, $text);
			$text = $percent;
			imagettftext($image_dst, 16, 0, $percent_x, $percent_y, $black, $font, $text);
		}
		
		
		imagejpeg($image_dst, $out_path, 100);
		//imagepng($image_dst, $out_path);
		
	 	return true;
	 }
	 
	 
	 
	 
	 //图片合成（1张）
	 public function combine_pic_single($background_path,$out_path,$org_path_1="",$dst_x_1="",$dst_y_1="",$score="",$percent="",$score_x="",$score_y="",$percent_x="",$percent_y=""){
	 	
		//$org_path ='D:/www/cuicuisha/gd_test/out.jpg'; //原始图片（此处原始图，即之前裁切后的图片）
		//$background_path = "D:/www/cuicuisha/gd_test/back.png";  //底图
		//$out_path = 'D:/www/cuicuisha/gd_test/gamepic.jpg';  //目标图片

		//$dst_x=550;  //原始图坐落到背景画布的坐标x（根据需求写死），以左上角为基点
		//$dst_y=250;  //原始图坐落到背景画布的坐标y（根据需求写死），以左上角为基点
		
		//叠加图片及图片合成

		$img_background_info=getimagesize($background_path);
		//echo "<pre>";print_r($img_background_info);exit;
		$bg_width=$img_background_info[0];
		$bg_height=$img_background_info[1];


		$img_org_info=getimagesize($org_path_1);
		$org_width_1=$img_org_info[0];
		$org_height_1=$img_org_info[1];

		
		$image_dst = imagecreatetruecolor($bg_width, $bg_height);  //创建一个新的目标图像

		//imagealphablending($image_dst,false);  //合并图像的时候，这句千万不能加上，否则合并出来的上层图片会遮挡下层图片。
		imagesavealpha($image_dst,true);  //这里很重要,意思是不要丢了$thumb图像的透明色;

		$white=imagecolorallocate($image_dst,255,255,255); 
		$black=imagecolorallocate($image_dst,0,0,0); 
		$red=imagecolorallocate($image_dst,255,0,0); 
		imagefill($image_dst,0,0,$white); 

		imagejpeg($image_dst, $out_path, 100);
		
		if($org_path_1!="" && $dst_x_1!="" && $dst_y_1!=""){
			//合并第1张原始图
			//echo $dst_x_1;echo "<br>";
			//echo $dst_y_1;echo "<br>";
			//echo $org_width_1;echo "<br>";
			//echo $org_height_1;echo "<br>";echo "<br>";
			$img_org = @imagecreatefromjpeg($org_path_1);  //从原始图获取所有内容
			imagecopy($image_dst,$img_org,$dst_x_1,$dst_y_1,0,0,$org_width_1,$org_height_1);  //将原始图叠加到背景画布上
		}
		

		$img_bg = @imagecreatefrompng($background_path);  //从原始图获取所有内容
		imagecopy($image_dst,$img_bg,0,0,0,0,$bg_width,$bg_height);  //将原始图叠加到背景画布上
		
		
		//无法指定文字大小和字体的做法：
		//$textcolor = imagecolorallocate($im, 0, 0, 255);  //水印文字
		//imagestring($image_dst, 4, 100, 100, "286", $textcolor);
		//$textcolor = imagecolorallocate($im, 0, 0, 255);  //水印文字
		//imagestring($image_dst, 4, 200, 200, "10%", $textcolor);
		
		
		if($score!=""){
			//可指定文字大小和字体的做法：
			$black = imagecolorallocate($im, 0, 0, 0);
			$font = ROOT_PATH.'/statics/font/arial.ttf';
			//image 图像资源 , size 字体大小 , angle 角度 , x , y , color , fontfile , text 
			$text = $score;
			imagettftext($image_dst, 16, 0, $score_x, $score_y, $black, $font, $text);
			$text = $percent;
			imagettftext($image_dst, 16, 0, $percent_x, $percent_y, $black, $font, $text);
		}
		
		
		imagejpeg($image_dst, $out_path, 100);
		//imagepng($image_dst, $out_path);
		
	 	return true;
	 }
	 
	 
	 //生成相册图
	 public function makealbum(){
	 	if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }
        
        
		$CityMod = M('game');
        $userinfo = $CityMod->field('id as game_id
        ,head1egg
        ,head2egg
        ,head3egg
        ')->where(" id='".$game_id."' " )->order('id desc')->limit('0,1')->select();
        if(isset($userinfo[0])){
        	$userinfo=$userinfo[0];
        	//echo "<pre>";print_r($userinfo);exit;
        	
        	$org_path_1=empty($userinfo['head1egg'])?"":BASE_PIC_RESIZE_PATH.$userinfo['head1egg'];
			$org_path_2=empty($userinfo['head2egg'])?"":BASE_PIC_RESIZE_PATH.$userinfo['head2egg'];
			$org_path_3=empty($userinfo['head3egg'])?"":BASE_PIC_RESIZE_PATH.$userinfo['head3egg'];
			
        	if($userinfo['head1egg']=="" && $userinfo['head2egg']=="" && $userinfo['head3egg']=="" ){
	        	$data['game_id']=$game_id;
	            $this->jsonData(1,'失败',$data);
	            exit;
        	}
        	
        	$album_arr[]=1;
	        $album_arr[]=2;
	        $album_arr[]=3;
	        $album_arr[]=4;
	        $album_arr[]=5;
	        
	        //循环遍历5个相册底图
	        foreach ($album_arr as $k => $v) {
	        	
	        	$album_no=$v;
	        	
	        	if($userinfo['head1egg']=="" && $userinfo['head2egg']=="" && $userinfo['head3egg']!="" ){
					$background_path=BASE_PIC_BACKGROUND_ALBUM_PATH.'album'.$album_no.'/'.'album_'.$album_no.'_role_0_0_1.png';
				}
				elseif($userinfo['head1egg']=="" && $userinfo['head2egg']!="" && $userinfo['head3egg']=="" ){
					$background_path=BASE_PIC_BACKGROUND_ALBUM_PATH.'album'.$album_no.'/'.'album_'.$album_no.'_role_0_1_0.png';
				}
				elseif($userinfo['head1egg']=="" && $userinfo['head2egg']!="" && $userinfo['head3egg']!="" ){
					$background_path=BASE_PIC_BACKGROUND_ALBUM_PATH.'album'.$album_no.'/'.'album_'.$album_no.'_role_0_1_1.png';
				}
				elseif($userinfo['head1egg']!="" && $userinfo['head2egg']=="" && $userinfo['head3egg']=="" ){
					$background_path=BASE_PIC_BACKGROUND_ALBUM_PATH.'album'.$album_no.'/'.'album_'.$album_no.'_role_1_0_0.png';
				}
				elseif($userinfo['head1egg']!="" && $userinfo['head2egg']=="" && $userinfo['head3egg']!="" ){
					$background_path=BASE_PIC_BACKGROUND_ALBUM_PATH.'album'.$album_no.'/'.'album_'.$album_no.'_role_1_0_1.png';
				}
				elseif($userinfo['head1egg']!="" && $userinfo['head2egg']!="" && $userinfo['head3egg']=="" ){
					$background_path=BASE_PIC_BACKGROUND_ALBUM_PATH.'album'.$album_no.'/'.'album_'.$album_no.'_role_1_1_0.png';
				}
				elseif($userinfo['head1egg']!="" && $userinfo['head2egg']!="" && $userinfo['head3egg']!="" ){
					$background_path=BASE_PIC_BACKGROUND_ALBUM_PATH.'album'.$album_no.'/'.'album_'.$album_no.'_role_1_1_1.png';
				}
				else{
					$this->jsonData(1,'缺少头像图');
		            exit;
				}
				
				
				
				if($album_no==1){
					//这里还分7这种情况
					if($userinfo['head1egg']=="" && $userinfo['head2egg']=="" && $userinfo['head3egg']!="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=250; //小孩坐标，以左上角为基点
						$dst_y_3=635; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']=="" && $userinfo['head2egg']!="" && $userinfo['head3egg']=="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=385; //母亲坐标，以左上角为基点
						$dst_y_2=530; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']=="" && $userinfo['head2egg']!="" && $userinfo['head3egg']!="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=395; //母亲坐标，以左上角为基点
						$dst_y_2=520; //母亲坐标，以左上角为基点
						$dst_x_3=260; //小孩坐标，以左上角为基点
						$dst_y_3=665; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']=="" && $userinfo['head3egg']=="" ){
						$dst_x_1=70; //父亲坐标，以左上角为基点
						$dst_y_1=512; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']=="" && $userinfo['head3egg']!="" ){
						$dst_x_1=65; //父亲坐标，以左上角为基点
						$dst_y_1=502; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=245; //小孩坐标，以左上角为基点
						$dst_y_3=640; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']!="" && $userinfo['head3egg']=="" ){
						$dst_x_1=75; //父亲坐标，以左上角为基点
						$dst_y_1=512; //父亲坐标，以左上角为基点
						$dst_x_2=385; //母亲坐标，以左上角为基点
						$dst_y_2=530; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']!="" && $userinfo['head3egg']!="" ){
						$dst_x_1=80; //父亲坐标，以左上角为基点
						$dst_y_1=522; //父亲坐标，以左上角为基点
						$dst_x_2=385; //母亲坐标，以左上角为基点
						$dst_y_2=510; //母亲坐标，以左上角为基点
						$dst_x_3=250; //小孩坐标，以左上角为基点
						$dst_y_3=675; //小孩坐标，以左上角为基点
					}
					else{
						$this->jsonData(1,'参数错误');
			            exit;
					}
				}
				
				
				if($album_no==2){
					//这里还分7这种情况
					if($userinfo['head1egg']=="" && $userinfo['head2egg']=="" && $userinfo['head3egg']!="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=260; //小孩坐标，以左上角为基点
						$dst_y_3=600; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']=="" && $userinfo['head2egg']!="" && $userinfo['head3egg']=="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=385; //母亲坐标，以左上角为基点
						$dst_y_2=465; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']=="" && $userinfo['head2egg']!="" && $userinfo['head3egg']!="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=385; //母亲坐标，以左上角为基点
						$dst_y_2=465; //母亲坐标，以左上角为基点
						$dst_x_3=280; //小孩坐标，以左上角为基点
						$dst_y_3=580; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']=="" && $userinfo['head3egg']=="" ){
						$dst_x_1=125; //父亲坐标，以左上角为基点
						$dst_y_1=445; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']=="" && $userinfo['head3egg']!="" ){
						$dst_x_1=115; //父亲坐标，以左上角为基点
						$dst_y_1=445; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=290; //小孩坐标，以左上角为基点
						$dst_y_3=580; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']!="" && $userinfo['head3egg']=="" ){
						$dst_x_1=130; //父亲坐标，以左上角为基点
						$dst_y_1=455; //父亲坐标，以左上角为基点
						$dst_x_2=380; //母亲坐标，以左上角为基点
						$dst_y_2=465; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']!="" && $userinfo['head3egg']!="" ){
						$dst_x_1=135; //父亲坐标，以左上角为基点
						$dst_y_1=455; //父亲坐标，以左上角为基点
						$dst_x_2=395; //母亲坐标，以左上角为基点
						$dst_y_2=465; //母亲坐标，以左上角为基点
						$dst_x_3=280; //小孩坐标，以左上角为基点
						$dst_y_3=570; //小孩坐标，以左上角为基点
					}
					else{
						$this->jsonData(1,'参数错误');
			            exit;
					}
				}
				
				
				if($album_no==3){
					//这里还分7这种情况
					if($userinfo['head1egg']=="" && $userinfo['head2egg']=="" && $userinfo['head3egg']!="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=250; //小孩坐标，以左上角为基点
						$dst_y_3=505; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']=="" && $userinfo['head2egg']!="" && $userinfo['head3egg']=="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=355; //母亲坐标，以左上角为基点
						$dst_y_2=420; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']=="" && $userinfo['head2egg']!="" && $userinfo['head3egg']!="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=365; //母亲坐标，以左上角为基点
						$dst_y_2=425; //母亲坐标，以左上角为基点
						$dst_x_3=250; //小孩坐标，以左上角为基点
						$dst_y_3=515; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']=="" && $userinfo['head3egg']=="" ){
						$dst_x_1=100; //父亲坐标，以左上角为基点
						$dst_y_1=445; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']=="" && $userinfo['head3egg']!="" ){
						$dst_x_1=95; //父亲坐标，以左上角为基点
						$dst_y_1=435; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=250; //小孩坐标，以左上角为基点
						$dst_y_3=505; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']!="" && $userinfo['head3egg']=="" ){
						$dst_x_1=95; //父亲坐标，以左上角为基点
						$dst_y_1=455; //父亲坐标，以左上角为基点
						$dst_x_2=355; //母亲坐标，以左上角为基点
						$dst_y_2=425; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']!="" && $userinfo['head3egg']!="" ){
						$dst_x_1=85; //父亲坐标，以左上角为基点
						$dst_y_1=455; //父亲坐标，以左上角为基点
						$dst_x_2=395; //母亲坐标，以左上角为基点
						$dst_y_2=435; //母亲坐标，以左上角为基点
						$dst_x_3=250; //小孩坐标，以左上角为基点
						$dst_y_3=505; //小孩坐标，以左上角为基点
					}
					else{
						$this->jsonData(1,'参数错误');
			            exit;
					}
				}
				
				
				if($album_no==4){
					//这里还分7这种情况
					if($userinfo['head1egg']=="" && $userinfo['head2egg']=="" && $userinfo['head3egg']!="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=280; //小孩坐标，以左上角为基点
						$dst_y_3=515; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']=="" && $userinfo['head2egg']!="" && $userinfo['head3egg']=="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=385; //母亲坐标，以左上角为基点
						$dst_y_2=435; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']=="" && $userinfo['head2egg']!="" && $userinfo['head3egg']!="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=395; //母亲坐标，以左上角为基点
						$dst_y_2=435; //母亲坐标，以左上角为基点
						$dst_x_3=280; //小孩坐标，以左上角为基点
						$dst_y_3=525; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']=="" && $userinfo['head3egg']=="" ){
						$dst_x_1=135; //父亲坐标，以左上角为基点
						$dst_y_1=445; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']=="" && $userinfo['head3egg']!="" ){
						$dst_x_1=135; //父亲坐标，以左上角为基点
						$dst_y_1=445; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=280; //小孩坐标，以左上角为基点
						$dst_y_3=515; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']!="" && $userinfo['head3egg']=="" ){
						$dst_x_1=135; //父亲坐标，以左上角为基点
						$dst_y_1=465; //父亲坐标，以左上角为基点
						$dst_x_2=395; //母亲坐标，以左上角为基点
						$dst_y_2=435; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']!="" && $userinfo['head3egg']!="" ){
						$dst_x_1=125; //父亲坐标，以左上角为基点
						$dst_y_1=455; //父亲坐标，以左上角为基点
						$dst_x_2=410; //母亲坐标，以左上角为基点
						$dst_y_2=435; //母亲坐标，以左上角为基点
						$dst_x_3=270; //小孩坐标，以左上角为基点
						$dst_y_3=525; //小孩坐标，以左上角为基点
					}
					else{
						$this->jsonData(1,'参数错误');
			            exit;
					}
				}
				
				
				
				if($album_no==5){
					//这里还分7这种情况
					if($userinfo['head1egg']=="" && $userinfo['head2egg']=="" && $userinfo['head3egg']!="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=320; //小孩坐标，以左上角为基点
						$dst_y_3=510; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']=="" && $userinfo['head2egg']!="" && $userinfo['head3egg']=="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=415; //母亲坐标，以左上角为基点
						$dst_y_2=510; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']=="" && $userinfo['head2egg']!="" && $userinfo['head3egg']!="" ){
						$dst_x_1=""; //父亲坐标，以左上角为基点
						$dst_y_1=""; //父亲坐标，以左上角为基点
						$dst_x_2=380; //母亲坐标，以左上角为基点
						$dst_y_2=500; //母亲坐标，以左上角为基点
						$dst_x_3=315; //小孩坐标，以左上角为基点
						$dst_y_3=525; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']=="" && $userinfo['head3egg']=="" ){
						$dst_x_1=170; //父亲坐标，以左上角为基点
						$dst_y_1=445; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']=="" && $userinfo['head3egg']!="" ){
						$dst_x_1=160; //父亲坐标，以左上角为基点
						$dst_y_1=455; //父亲坐标，以左上角为基点
						$dst_x_2=""; //母亲坐标，以左上角为基点
						$dst_y_2=""; //母亲坐标，以左上角为基点
						$dst_x_3=295; //小孩坐标，以左上角为基点
						$dst_y_3=535; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']!="" && $userinfo['head3egg']=="" ){
						$dst_x_1=180; //父亲坐标，以左上角为基点
						$dst_y_1=455; //父亲坐标，以左上角为基点
						$dst_x_2=420; //母亲坐标，以左上角为基点
						$dst_y_2=500; //母亲坐标，以左上角为基点
						$dst_x_3=""; //小孩坐标，以左上角为基点
						$dst_y_3=""; //小孩坐标，以左上角为基点
					}
					elseif($userinfo['head1egg']!="" && $userinfo['head2egg']!="" && $userinfo['head3egg']!="" ){
						$dst_x_1=150; //父亲坐标，以左上角为基点
						$dst_y_1=445; //父亲坐标，以左上角为基点
						$dst_x_2=430; //母亲坐标，以左上角为基点
						$dst_y_2=500; //母亲坐标，以左上角为基点
						$dst_x_3=300; //小孩坐标，以左上角为基点
						$dst_y_3=525; //小孩坐标，以左上角为基点
					}
					else{
						$this->jsonData(1,'参数错误');
			            exit;
					}
				}
				
				
				$path=$game_id."_album_".$album_no."_".date('ymdHis')."_".rand(10,99).".png";
				
				$out_path=BASE_PIC_ALBUM_PATH.$path;
				
				$album_rst=$this->combine_pic_multi($background_path,$out_path,$org_path_1,$dst_x_1,$dst_y_1,$org_path_2,$dst_x_2,$dst_y_2,$org_path_3,$dst_x_3,$dst_y_3);
				
				if($album_rst==true){
			        $UserMod = M('game');
			        $sql=sprintf("UPDATE %s SET album".addslashes($album_no)."='".$path."' where id='".addslashes($game_id)."' ", $UserMod->getTableName() );
			        $result = $UserMod->execute($sql);
			        
			        $key_album='album'.addslashes($album_no);
			        $data[$key_album]=BASE_URL."/public/web_album/".$path;
			        
		        }
		        else{
		        	$this->jsonData(1,'生成相册失败');
		            exit;
		        }
		        
			}
			
			
			if(isset($data['album1'])){
				$data['game_id']=$game_id;
				$this->jsonData(0,'成功',$data);
				exit;
			}
			else{
				$data['game_id']=$game_id;
	            $this->jsonData(1,'失败',$data);
	            exit;
			}
        }
        else{
        	$data['game_id']=$game_id;
            $this->jsonData(1,'失败',$data);
            exit;
        }
		
		exit;
	 }
	 
	 
	 
	 //重新玩游戏，清理SESSION
	 public function clear_game(){
	 	if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            unset($_SESSION['game_id']);
        }
	 }
	
	
	
	
	//上传人物原始图(js已经提交到微信服务器，请求php去微信服务器获取) test测试  直接请求一个图片url获取并保存图片的实例
    public function uploadheadpic_weixin_test(){
		
		$openid='xxxx';
        $game_id=1;
        //http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=sw4IQ9_TEZZCwAL-DRVsLr0VHBgBOAhLROAdZ9i2GL2DkomMIC6GaMaL6kACdxxYJ_utk1wDCa_UhIzeaM6NNDeDUMmw_sn8GMQaH8VoLjg&media_id=BFjezF-HCz6YwCVYqHKOSb0dSbxHpEFrNINsQgK8YuGA7PZSuPAkeOAedbWoyrCE
		//http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=OezXcEiiBSKSxW0eoylIeOLdVqgl91CQfORI5kdcor6Hwxt5mhBZ0V8JygyC3mmvYNBWENs_7a2WqVzzspPdNzfWzorF9U0pQU02ktJhaebyYgUVV5bO1SHseIeAenJ2lmaGWiuclVOTeeNP1xIC1g&media_id=BFjezF-HCz6YwCVYqHKOSb0dSbxHpEFrNINsQgK8YuGA7PZSuPAkeOAedbWoyrCE
		
        $media_id='BFjezF-HCz6YwCVYqHKOSb0dSbxHpEFrNINsQgK8YuGA7PZSuPAkeOAedbWoyrCE';
        echo $media_id;echo "<br><br>";
        
		if(1==1){
			
			echo "now:".$_SESSION['WX_INFO']['access_token'];echo "<br><br>";
			
			//第三步：刷新access_token（如果需要）
			//$get_url='https://ruby-china-files.b0.upaiyun.com/user/large_avatar/871.jpg';
			//$get_url = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token='.$_SESSION['WX_INFO']['access_token'].'&media_id='.$media_id.'';
			$get_url = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=sw4IQ9_TEZZCwAL-DRVsLr0VHBgBOAhLROAdZ9i2GL2DkomMIC6GaMaL6kACdxxYJ_utk1wDCa_UhIzeaM6NNDeDUMmw_sn8GMQaH8VoLjg&media_id=BFjezF-HCz6YwCVYqHKOSb0dSbxHpEFrNINsQgK8YuGA7PZSuPAkeOAedbWoyrCE';
			echo $get_url;echo "<br><br>";
			$get_return = file_get_contents($get_url);
			//$get_return = (array)json_decode($get_return);
			//echo "<pre>";print_r($get_return);
			
			var_dump($get_return);echo "<br><br>";
			
			$is_android=0;
        	
        	
			$path = $game_id."_head_".date('ymdHis')."_".rand(10,99).".png";
			$dest = BASE_UPLOAD_PATH.$path;
			
			echo $dest;echo "<br><br>";
			
			if(!empty($get_return)){
				
				//模仿复制 php://input 模式上传
				$f = fopen($dest,'w');
				fwrite($f,$get_return);
				fclose($f);
				
				
				$exif = exif_read_data($dest);
				$ort = isset($exif['Orientation'])?$exif['Orientation']:"";
				if($ort==""){
					$ort = isset($exif['IFD0']['Orientation'])?$exif['IFD0']['Orientation']:"";
				}
				
				switch($ort){
					case 1: // nothing
						break;
	    			case 2: // horizontal flip
	    				break;
	                case 3: // 180 rotate left  //向左旋转180度
	                    //$image->imagerotate($upload_path . $newfilename, 180, -1);
	                    $pic_imagerotate='180';
	                    break;
	                case 4: // vertical flip
	        			break;
	        		case 5: // vertical flip + 90 rotate right
				        $pic_imagerotate='-90';
				        break;
	                case 6: // 90 rotate right  //向右旋转90度
	                    //$image->imagerotate($upload_path . $newfilename, -90, -1);
	                    $pic_imagerotate='-90';
	                    break;
	                case 7: // horizontal flip + 90 rotate right
				        $pic_imagerotate='-90';
				        break;
	                case 8:    // 90 rotate left  //向左旋转90度
	                    //$image->imagerotate($upload_path . $newfilename, 90, -1);
	                    $pic_imagerotate='90';
	                    break;
	            }
				
				
				$file_type=$this->get_file_type($dest);
				if ($file_type!="jpg" && $file_type!="gif" && $file_type!="png"){
					@unlink($dest);
					$this->jsonData(1,'图片上传限定jpg、gif、png类型');
				    exit;
				}
				
				//if($debug==1){
				//	echo "get file type:";
				//	echo $file_type;
				//	echo "<br>";
				//}
				
				
				$file_z=filesize($dest);
				$f_size_limit_byte=BASE_FILE_SIZE_LIMIT*1024*1024;
				
				if ($file_z>$f_size_limit_byte){
					@unlink($dest);
					$this->jsonData(1,'图片上传限定'.BASE_FILE_SIZE_LIMIT.'M');
				    exit;
				}
				
				
				
				$UserMod = M('game');
		        $sql=sprintf("UPDATE %s SET headpic='".addslashes($path)."' , imagerotate='".addslashes($pic_imagerotate)."' where id='".addslashes($game_id)."' ", $UserMod->getTableName() );
		        $result = $UserMod->execute($sql);
		        
		        echo $sql;echo "<br><br>";
		        
		        //if($debug==1){
				//	echo "sql:";
				//	echo $sql;
				//	echo "<br>";
				//}
				
		        
		        //if($debug==1){
				//	exit;
				//}
				
				exit;
				
		        
				$data['game_id']=$game_id;
				$data['pic_url']=BASE_URL."/public/web_pic/".$path;
		        $this->jsonData(0,'成功',$data);
	        }
	        
		}
		
		$this->jsonData(0,'失败');
        
    }
    
    
    
    //微信  媒体文件 上传 (不需要做，因为：就是因为安卓无法上传，所以才需要让js去实现这块技术)
    public function uploadheadpic_weixin_upload(){
    	
    	
    	if(isset($_SESSION['WX_INFO']['openid']) && $_SESSION['WX_INFO']['openid']!=""){
            $openid=$_SESSION['WX_INFO']['openid'];
        }
        else{
        	$this->jsonData(1,'请先授权微信认证');
            exit;
        }
        
        
        if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }
        
    }
    
    
    
    //上传人物原始图(js已经提交到微信服务器，请求php去微信服务器获取)
    public function uploadheadpic_weixin(){
		
		
    	if(isset($_SESSION['WX_INFO']['openid']) && $_SESSION['WX_INFO']['openid']!=""){
            $openid=$_SESSION['WX_INFO']['openid'];
        }
        else{
        	$this->jsonData(1,'请先授权微信认证');
            exit;
        }
        
        
        if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }
        
        
        if(isset($_REQUEST['media_id']) && $_REQUEST['media_id']!=""){
            $media_id=$_REQUEST['media_id'];
        }
        else{
            $media_id=0;
        }
        
        if(isset($_REQUEST['type']) && $_REQUEST['type']!=""){
            $type=$_REQUEST['type'];
        }
        else{
            $type=".png";
        }
        
        if(isset($_REQUEST['token']) && $_REQUEST['token']!=""){
            $token=$_REQUEST['token'];
        }
        else{
            $token="";
        }
        
        
        if(isset($_REQUEST['debug']) && $_REQUEST['debug']==1){
            $debug=$_REQUEST['debug'];
        }
        else{
            $debug=0;
        }
        
        
		if(isset($_SESSION['WX_INFO']['access_token']) && $_SESSION['WX_INFO']['access_token']!=""){
			
			//第三步：刷新access_token（如果需要）
			$get_url = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token='.$token.'&media_id='.$media_id.'';
			$get_return = file_get_contents($get_url);
			//$get_return = (array)json_decode($get_return);
			//echo "<pre>";print_r($get_return);
			
        	
        	
			$path = $game_id."_head_".date('ymdHis')."_".rand(10,99).$type;
			$dest = BASE_UPLOAD_PATH.$path;
			
			if(!empty($get_return)){
				
				//$this->jsonData(1,'文件内容:'.$get_return);
				//exit;
				
				//模仿复制 php://input 模式上传
				$f = fopen($dest,'w');
				fwrite($f,$get_return);
				fclose($f);
				
				
				$exif = exif_read_data($dest);
				$ort = isset($exif['Orientation'])?$exif['Orientation']:"";
				if($ort==""){
					$ort = isset($exif['IFD0']['Orientation'])?$exif['IFD0']['Orientation']:"";
				}
				
				switch($ort){
					case 1: // nothing
						break;
	    			case 2: // horizontal flip
	    				break;
	                case 3: // 180 rotate left  //向左旋转180度
	                    //$image->imagerotate($upload_path . $newfilename, 180, -1);
	                    $pic_imagerotate='180';
	                    break;
	                case 4: // vertical flip
	        			break;
	        		case 5: // vertical flip + 90 rotate right
				        $pic_imagerotate='-90';
				        break;
	                case 6: // 90 rotate right  //向右旋转90度
	                    //$image->imagerotate($upload_path . $newfilename, -90, -1);
	                    $pic_imagerotate='-90';
	                    break;
	                case 7: // horizontal flip + 90 rotate right
				        $pic_imagerotate='-90';
				        break;
	                case 8:    // 90 rotate left  //向左旋转90度
	                    //$image->imagerotate($upload_path . $newfilename, 90, -1);
	                    $pic_imagerotate='90';
	                    break;
	            }
				
				
				$file_type=$this->get_file_type($dest);
				if ($file_type!="jpg" && $file_type!="gif" && $file_type!="png"){
					
					//$this->jsonData(1,'图片上传限定jpg、gif、png类型:'.$dest.'。文件内容:'.$get_return.'。接口url：'.$get_url);
				    //exit;
				    
					@unlink($dest);
					$this->jsonData(1,'图片上传限定jpg、gif、png类型');
				    exit;
				}
				
				//if($debug==1){
				//	echo "get file type:";
				//	echo $file_type;
				//	echo "<br>";
				//}
				
				
				$file_z=filesize($dest);
				$f_size_limit_byte=BASE_FILE_SIZE_LIMIT*1024*1024;
				
				if ($file_z>$f_size_limit_byte){
					
					@unlink($dest);
					$this->jsonData(1,'图片上传限定'.BASE_FILE_SIZE_LIMIT.'M');
				    exit;
				}
				
				
				
				$UserMod = M('game');
		        $sql=sprintf("UPDATE %s SET headpic='".addslashes($path)."' , imagerotate='".addslashes($pic_imagerotate)."' where id='".addslashes($game_id)."' ", $UserMod->getTableName() );
		        $result = $UserMod->execute($sql);
		        
		        //if($debug==1){
				//	echo "sql:";
				//	echo $sql;
				//	echo "<br>";
				//}
				
		        
		        //if($debug==1){
				//	exit;
				//}
				
		        
				$data['game_id']=$game_id;
				$data['pic_url']=BASE_URL."/public/web_pic/".$path;
		        $this->jsonData(0,'成功',$data);
	        }
	        else{
	        	$this->jsonData(1,'图片获取失败');
	        }
		}
		
		$this->jsonData(0,'失败');
        
    }
    
    
    
    
    //上传头像图
    public function uploadheadpic(){
		
		
        if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }
        
        
        if(isset($_REQUEST['is_android']) && $_REQUEST['is_android']==1){
            $is_android=$_REQUEST['is_android'];
        }
        else{
            $is_android=0;
        }
        
        
        
		$path = $game_id."_head_".date('ymdHis')."_".rand(10,99).".png";
		$dest = BASE_UPLOAD_PATH.$path;
		
		/*
		//base64模式接受上传数据
		$f = fopen($dest,'w');
		$img = '';
		$img = str_replace('data:image/jpeg;base64,', '', $img);
		$img = str_replace('data:image/gif;base64,', '', $img);
		$img = str_replace('data:image/png;base64,', '', $img);
		$img = str_replace(' ', '+', $img);
		$datas = base64_decode($img);
		fwrite($f,$datas);
		fclose($f);
		*/
		
		
		/*
		用 $_FILES 接收，可解决微信摄像头问题。前端是用 FormData 技术提交过来的。
		*/
		
		
		//if(isset($_REQUEST['debug']) && $_REQUEST['debug']==1){
        //    $debug=$_REQUEST['debug'];
        //}
        //else{
        //    $debug=0;
        //}
        
        //if($debug==1){
	        
	        //echo "FILES:";echo "<br>";
	        //echo "<pre>";print_r($_FILES);echo "</pre>";
	        //echo "<br>";
	        
	        //echo "POST:";echo "<br>";
	        //echo "<pre>";print_r($_POST);echo "</pre>";
	        //echo "<br>";
	        
	        //exit;
        
        //}
        
		
		
		$pic_imagerotate='';
		
		if($_FILES){
			//上传方式：表单
			$input = key($_FILES);
			
			//if($debug==1){
			//	echo "input:";
			//	echo $input;
			//	echo "<br>";
			//}
			
			
			if(!move_uploaded_file($_FILES[$input]['tmp_name'],$dest)) {
				
				//if($debug==1){
				//	echo "uploaded error:";
				//	echo "<br>";
				//}
				
				$data['game_id']=$game_id;
		        $this->jsonData(1,'失败');
		        exit;
			}
			else{
				
				//if($debug==1){
				//	echo "uploaded success:";
				//	echo $dest;
				//	echo "<br>";
				//}
				
				//判断EXIF头信息模式解决旋转90度问题
				$exif = exif_read_data($dest);
				$ort = isset($exif['Orientation'])?$exif['Orientation']:"";
				if($ort==""){
					$ort = isset($exif['IFD0']['Orientation'])?$exif['IFD0']['Orientation']:"";
				}
				switch($ort){
					case 1: // nothing
						break;
        			case 2: // horizontal flip
        				break;
	                case 3: // 180 rotate left  //向左旋转180度
	                    //$image->imagerotate($upload_path . $newfilename, 180, -1);
	                    $pic_imagerotate='180';
	                    break;
	                case 4: // vertical flip
            			break;
            		case 5: // vertical flip + 90 rotate right
				        $pic_imagerotate='-90';
				        break;
	                case 6: // 90 rotate right  //向右旋转90度
	                    //$image->imagerotate($upload_path . $newfilename, -90, -1);
	                    $pic_imagerotate='-90';
	                    break;
	                case 7: // horizontal flip + 90 rotate right
				        $pic_imagerotate='-90';
				        break;
	                case 8:    // 90 rotate left  //向左旋转90度
	                    //$image->imagerotate($upload_path . $newfilename, 90, -1);
	                    $pic_imagerotate='90';
	                    break;
	            }
	            //echo "<pre>";print_r($exif);exit;
	            //var_dump($pic_imagerotate);exit;
			}
		}
		elseif(isset($GLOBALS['HTTP_RAW_POST_DATA'])){ 
			//上传方式：原始POST
			//$f = fopen($dest,'w');
			//fwrite($f,$GLOBALS['HTTP_RAW_POST_DATA']);
			//fclose($f);
			
			//base64模式接受上传数据
			/*
			$f = fopen($dest,'w');
			$img = $GLOBALS['HTTP_RAW_POST_DATA'];
			$img = str_replace('data:image/jpeg;base64,', '', $img);
			$img = str_replace('data:image/gif;base64,', '', $img);
			$img = str_replace('data:image/png;base64,', '', $img);
			$img = str_replace(' ', '+', $img);
			$datas = base64_decode($img);
			fwrite($f,$datas);
			fclose($f);
			*/
			
			if($is_android==1){
				$f = fopen($dest,'w');
				$img = $GLOBALS['HTTP_RAW_POST_DATA'];
				$img = str_replace('data:image/jpeg;base64,', '', $img);
				$img = str_replace('data:image/gif;base64,', '', $img);
				$img = str_replace('data:image/png;base64,', '', $img);
				$img = str_replace(' ', '+', $img);
				$datas = base64_decode($img);
				fwrite($f,$datas);
				fclose($f);
			}
			else{
				//判断EXIF头信息模式解决旋转90度问题（示例：http://stackoverflow.com/questions/7489742/php-read-exif-data-and-adjust-orientation）
				$f = fopen($dest,'w');
				fwrite($f,$GLOBALS['HTTP_RAW_POST_DATA']);
				fclose($f);
			}
			
			$exif = exif_read_data($dest);
			$ort = isset($exif['Orientation'])?$exif['Orientation']:"";
			if($ort==""){
				$ort = isset($exif['IFD0']['Orientation'])?$exif['IFD0']['Orientation']:"";
			}
			switch($ort){
				case 1: // nothing
					break;
    			case 2: // horizontal flip
    				break;
                case 3: // 180 rotate left  //向左旋转180度
                    //$image->imagerotate($upload_path . $newfilename, 180, -1);
                    $pic_imagerotate='180';
                    break;
                case 4: // vertical flip
        			break;
        		case 5: // vertical flip + 90 rotate right
			        $pic_imagerotate='-90';
			        break;
                case 6: // 90 rotate right  //向右旋转90度
                    //$image->imagerotate($upload_path . $newfilename, -90, -1);
                    $pic_imagerotate='-90';
                    break;
                case 7: // horizontal flip + 90 rotate right
			        $pic_imagerotate='-90';
			        break;
                case 8:    // 90 rotate left  //向左旋转90度
                    //$image->imagerotate($upload_path . $newfilename, 90, -1);
                    $pic_imagerotate='90';
                    break;
            }
            
			
			
		}
		else{ 
			//上传方式：客户端提交
			//$f = fopen($dest,'w');
			//fwrite($f,file_get_contents('php://input'));
			//fclose($f);
			
			//base64模式接受上传数据
			/*
			$f = fopen($dest,'w');
			$img = file_get_contents('php://input');
			$img = str_replace('data:image/jpeg;base64,', '', $img);
			$img = str_replace('data:image/gif;base64,', '', $img);
			$img = str_replace('data:image/png;base64,', '', $img);
			$img = str_replace(' ', '+', $img);
			$datas = base64_decode($img);
			fwrite($f,$datas);
			fclose($f);
			*/
			
			if($is_android==1){
				$f = fopen($dest,'w');
				$img = file_get_contents('php://input');
				$img = str_replace('data:image/jpeg;base64,', '', $img);
				$img = str_replace('data:image/gif;base64,', '', $img);
				$img = str_replace('data:image/png;base64,', '', $img);
				$img = str_replace(' ', '+', $img);
				$datas = base64_decode($img);
				fwrite($f,$datas);
				fclose($f);
			}
			else{
				//判断EXIF头信息模式解决旋转90度问题
				$f = fopen($dest,'w');
				fwrite($f,file_get_contents('php://input'));
				fclose($f);
			}
			
			$exif = exif_read_data($dest);
			$ort = isset($exif['Orientation'])?$exif['Orientation']:"";
			if($ort==""){
				$ort = isset($exif['IFD0']['Orientation'])?$exif['IFD0']['Orientation']:"";
			}
			switch($ort){
				case 1: // nothing
					break;
    			case 2: // horizontal flip
    				break;
                case 3: // 180 rotate left  //向左旋转180度
                    //$image->imagerotate($upload_path . $newfilename, 180, -1);
                    $pic_imagerotate='180';
                    break;
                case 4: // vertical flip
        			break;
        		case 5: // vertical flip + 90 rotate right
			        $pic_imagerotate='-90';
			        break;
                case 6: // 90 rotate right  //向右旋转90度
                    //$image->imagerotate($upload_path . $newfilename, -90, -1);
                    $pic_imagerotate='-90';
                    break;
                case 7: // horizontal flip + 90 rotate right
			        $pic_imagerotate='-90';
			        break;
                case 8:    // 90 rotate left  //向左旋转90度
                    //$image->imagerotate($upload_path . $newfilename, 90, -1);
                    $pic_imagerotate='90';
                    break;
            }
			
			
			
		}
		
		
		//echo $exif['Orientation'];exit;
		//echo "<pre>";print_r($exif);exit;
		
		//模拟测试数据
		//$path='1684_head_150717212541_48.png';
		//$dest='D:\www\nac/public/web_pic/1684_head_150717212541_48.png';
		
		
		
		$file_type=$this->get_file_type($dest);
		if ($file_type!="jpg" && $file_type!="gif" && $file_type!="png"){
			@unlink($dest);
			$this->jsonData(1,'图片上传限定jpg、gif、png类型');
		    exit;
		}
		
		//if($debug==1){
		//	echo "get file type:";
		//	echo $file_type;
		//	echo "<br>";
		//}
		
		
		$file_z=filesize($dest);
		$f_size_limit_byte=BASE_FILE_SIZE_LIMIT*1024*1024;
		
		if ($file_z>$f_size_limit_byte){
			@unlink($dest);
			$this->jsonData(1,'图片上传限定'.BASE_FILE_SIZE_LIMIT.'M');
		    exit;
		}
		
		
		
		$UserMod = M('game');
        $sql=sprintf("UPDATE %s SET headpic='".addslashes($path)."' , imagerotate='".addslashes($pic_imagerotate)."' where id='".addslashes($game_id)."' ", $UserMod->getTableName() );
        $result = $UserMod->execute($sql);
        
        //if($debug==1){
		//	echo "sql:";
		//	echo $sql;
		//	echo "<br>";
		//}
		
        
        
        //if($debug==1){
		//	exit;
		//}
		
        
		$data['game_id']=$game_id;
		$data['pic_url']=BASE_URL."/public/web_pic/".$path;
        $this->jsonData(0,'成功',$data);
        
    }
    
    
    
    
    
    //对人物图片做裁切
    public function resizeheadpic(){
		
		
        if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }
        
        
		if(isset($_REQUEST['drag_w']) && isset($_REQUEST['drag_h']) && isset($_REQUEST['src_x']) && isset($_REQUEST['src_y']) && isset($_REQUEST['src_w']) && isset($_REQUEST['src_h']) ){
			$drag_w=$_REQUEST['drag_w'];
			$drag_h=$_REQUEST['drag_h'];
			$src_x=$_REQUEST['src_x'];
			$src_y=$_REQUEST['src_y'];
			$src_w=$_REQUEST['src_w'];
			$src_h=$_REQUEST['src_h'];
			$dst_w=isset($_REQUEST['dst_w'])?$_REQUEST['dst_w']:$src_w;
			$dst_h=isset($_REQUEST['dst_h'])?$_REQUEST['dst_h']:$src_h;
		}
		else{
			$this->jsonData(1,'参数错误');
            exit;
		}
		
		
        
        //判断当前角色的原始图是否已经上传
        $CityMod = M('game');
        $userinfo = $CityMod->field('id,headpic')->where(" id='".addslashes($game_id)."' " )->order('id desc')->limit('0,1')->select();
        //echo "<pre>";print_r($userinfo[0]);exit;
        if(isset($userinfo[0])){
        	$userinfo=$userinfo[0];
        	if(!empty($userinfo['headpic'])){
        		$org_path=BASE_UPLOAD_PATH.$userinfo['headpic'];
        	}
        	else{
	        	$this->jsonData(1,'请先上传原始图');
	            exit;
        	}
        	
        	$path=$game_id."_resize_".date('ymdHis')."_".rand(10,99).".png";
        	$out_path = BASE_PIC_RESIZE_PATH.$path;
        	//echo $out_path;exit;
        	
			$exif = exif_read_data($org_path);
			$ort = isset($exif['Orientation'])?$exif['Orientation']:"";
			if($ort==""){
				$ort = isset($exif['IFD0']['Orientation'])?$exif['IFD0']['Orientation']:"";
			}
			switch($ort){
				case 1: // nothing
					
					break;
    			case 2: // horizontal flip
    				break;
                case 3: // 180 rotate left  //向左旋转180度
                    //$image->imagerotate($upload_path . $newfilename, 180, -1);
                    $pic_imagerotate='180';
                    break;
                case 4: // vertical flip
        			break;
        		case 5: // vertical flip + 90 rotate right
			        $pic_imagerotate='-90';
			        break;
                case 6: // 90 rotate right  //向右旋转90度
                    //$image->imagerotate($upload_path . $newfilename, -90, -1);
                    $pic_imagerotate='-90';
                    break;
                case 7: // horizontal flip + 90 rotate right
			        $pic_imagerotate='-90';
			        break;
                case 8:    // 90 rotate left  //向左旋转90度
                    //$image->imagerotate($upload_path . $newfilename, 90, -1);
                    $pic_imagerotate='90';
                    break;
            }
            
            //测试：强制旋转xx角度
            //$pic_imagerotate='90';
            $resize_rst=$this->resize_pic($org_path,$out_path,$src_x,$src_y,$src_w,$src_h,$dst_w,$dst_h,$drag_w,$drag_h,$pic_imagerotate);
        	
        	sleep(1);
        	
        	if($resize_rst==true){
        		$UserMod = M('game');
		        $sql=sprintf("UPDATE %s SET headegg='".addslashes($path)."' where id='".addslashes($game_id)."' ", $UserMod->getTableName() );
		        $result = $UserMod->execute($sql);
		        
				$data['game_id']=$game_id;
				$data['pic_url']=BASE_URL."/public/web_resize/".$path;
		        $this->jsonData(0,'成功',$data);
        	}
        	else{
        		$this->jsonData(1,'图片裁切失败');
            	exit;
        	}
        	
        }
        else{
        	$this->jsonData(1,'请先参与游戏');
            exit;
        }
        
    }
    
    
    //裁切图片(前端不做drag拉伸缩放)
    public function resize_pic_not_drag($org_path,$out_path,$src_x,$src_y,$src_w,$src_h,$dst_w="",$dst_h=""){
		
		//$out_path = 'D:/www/cuicuisha/gd_test/out.jpg';  //目标图片
		//$org_path ='D:/www/cuicuisha/gd_test/1.jpg'; //原始图片

		//$src_x=10; //原始图的x坐标，以左上角为基点（前端传入）
		//$src_y=10; //原始图的y坐标，以左上角为基点（前端传入）
		//$src_w=30; //原始图截取的width（前端传入）
		//$src_h=40; //原始图截取的height（前端传入）

		$dst_x=0; //目标图的x坐标，固定写0
		$dst_y=0; //目标图的y坐标，固定写0
		//$dst_w=30; //目标图重新缩放的width，同时也是目标图对象的width，如此值等于src_w，则不失真，否则一定是失真的。（前端传入）
		//$dst_h=40; //目标图重新缩放的height，同时也是目标图对象的width，如此值等于src_h，则不失真，否则一定是失真的。（前端传入）

		if(empty($dst_w)){
			$dst_w=$src_w;
		}
		
		if(empty($dst_h)){
			$dst_h=$src_h;
		}
		
		
		//裁切原始图片
		
		$img_org_info=getimagesize($org_path); 
		//echo "<pre>";print_r($img_org_info);exit;

		switch ($img_org_info[2]) { 
		    case 1: 
				//如果是GIF图片，直接缩略成背景色是白色的GIF图片
				//$rst_for_gif=ImageResize($org_path,$width,$height,$outputname);
				//if ($rst_for_gif=="success"){
				//return $rst_for_gif;
				//}
				//$bgcolor = ImageColorTransparent($image,$bgcolor) ;
				
				$image =imagecreatefromgif($org_path); 
				break; 
		    
		    case 2: 
			    $image =imagecreatefromjpeg($org_path); 
			    break; 
		    
		    case 3: 
			    $image =imagecreatefrompng($org_path); 
			    break; 
		    
		    default: 
			    return "请上传jpg、gif、或png格式的文件";
			    
		}
		
		
		$width=$dst_w;
		$height=$dst_h;
		$image_dst = imagecreatetruecolor($width, $height);  //创建一个新的目标图像
		imagealphablending($image_dst,false);  //这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
		imagesavealpha($image_dst,true);  //这里很重要,意思是不要丢了$thumb图像的透明色;
		
		$white=imagecolorallocate($image_dst,255,255,255); 
		$black=imagecolorallocate($image_dst,0,0,0); 
		$red=imagecolorallocate($image_dst,255,0,0); 
		imagefill($image_dst,0,0,$white); 
		
		
		//复制图像的部分区块并调整大小
		//参数含义：$dst_image , $src_image , $dst_x , $dst_y , $src_x , $src_y , $dst_w , $dst_h , $src_w , $src_h 以左上角为基点
		
		imagecopyresampled($image_dst, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
		//imagejpeg($image_dst, $out_path, 100);
		imagepng($image_dst, $out_path);
		
		//echo 'resize success';
		return true;
    }
    
    
    //裁切图片(前端先drag拉伸缩放过)
    public function resize_pic($org_path,$out_path,$src_x,$src_y,$src_w,$src_h,$dst_w="",$dst_h="",$drag_w="",$drag_h="",$pic_imagerotate=""){
		
		//$out_path = 'D:/www/cuicuisha/gd_test/out.jpg';  //目标图片
		//$org_path ='D:/www/cuicuisha/gd_test/1.jpg'; //原始图片

		//$src_x=10; //原始图的x坐标，以左上角为基点（前端传入）,是滤镜放大/缩小后的x坐标
		//$src_y=10; //原始图的y坐标，以左上角为基点（前端传入）,是滤镜放大/缩小后的y坐标
		//$src_w=30; //原始图截取的width（前端传入）,是滤镜放大/缩小后截取选区的宽
		//$src_h=40; //原始图截取的height（前端传入）,是滤镜放大/缩小后的坐标截取选区的高

		$dst_x=0; //目标图的x坐标，固定写0
		$dst_y=0; //目标图的y坐标，固定写0
		//$dst_w=30; //目标图重新缩放的width，同时也是目标图对象的width。如不传，默认取src_w。
		//$dst_h=40; //目标图重新缩放的height，同时也是目标图对象的width。如不传，默认取src_y。

		if(empty($dst_w)){
			$dst_w=$src_w;
		}
		
		if(empty($dst_h)){
			$dst_h=$src_h;
		}
		
		
		//先按滤镜drag定的宽高缩放原始图
		$image_drag = imagecreatetruecolor($drag_w, $drag_h);  //创建一个新的目标图像
		imagealphablending($image_drag,false);  //这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
		imagesavealpha($image_drag,true);  //这里很重要,意思是不要丢了$thumb图像的透明色;
		
		$white=imagecolorallocate($image_drag,255,255,255); 
		$black=imagecolorallocate($image_drag,0,0,0); 
		$red=imagecolorallocate($image_drag,255,0,0); 
		imagefill($image_drag,0,0,$white); 
		
		
		
		
		
		//缩放到drag图里
		
		$img_org_info=getimagesize($org_path); 
		//echo "<pre>";print_r($img_org_info);exit;
		
		$org_width=$img_org_info[0];
		$org_height=$img_org_info[1];
		
		switch ($img_org_info[2]) { 
		    case 1: 
				//如果是GIF图片，直接缩略成背景色是白色的GIF图片
				//$rst_for_gif=ImageResize($org_path,$width,$height,$outputname);
				//if ($rst_for_gif=="success"){
				//return $rst_for_gif;
				//}
				//$bgcolor = ImageColorTransparent($image,$bgcolor) ;
				
				$image =imagecreatefromgif($org_path); 
				break; 
		    
		    case 2: 
		    	
			    $image =imagecreatefromjpeg($org_path); 
			    break; 
		    
		    case 3: 
		    	
			    $image =imagecreatefrompng($org_path); 
			    break; 
		    
		    default: 
			    return "请上传jpg、gif、或png格式的文件";
			    
		}
		
		//旋转角度
		/*
		$filename = 'D:/www/cuicuisha/public/web_resize/test.png';
		$degrees = 180;
		//header('Content-type: image/png');
		$source = imagecreatefrompng($filename);
		$rotate = imagerotate($source, $degrees, 0);
		//imagepng($rotate);
		imagepng($rotate, $out_path); 
		exit;
		*/
		
		
		//$pic_imagerotate=180;
		if($pic_imagerotate!=""){
			$org_path_rotate=$org_path."._temp_.png";
			$rotate = imagerotate($image, $pic_imagerotate, 0);
			imagepng($rotate, $org_path_rotate);
			//echo $org_path_rotate;exit;
			sleep(1);
			
			$org_path=$org_path_rotate;
			
			$img_org_info=getimagesize($org_path); 
			//echo "<pre>";print_r($img_org_info);exit;
			
			$org_width=$img_org_info[0];
			$org_height=$img_org_info[1];
			
			switch ($img_org_info[2]) { 
			    case 1: 
					//如果是GIF图片，直接缩略成背景色是白色的GIF图片
					//$rst_for_gif=ImageResize($org_path,$width,$height,$outputname);
					//if ($rst_for_gif=="success"){
					//return $rst_for_gif;
					//}
					//$bgcolor = ImageColorTransparent($image,$bgcolor) ;
					
					$image =imagecreatefromgif($org_path); 
					break; 
			    
			    case 2: 
			    	
				    $image =imagecreatefromjpeg($org_path); 
				    break; 
			    
			    case 3: 
			    	
				    $image =imagecreatefrompng($org_path); 
				    break; 
			    
			    default: 
				    return "请上传jpg、gif、或png格式的文件";
				    
			}
		}
		
		
		imagecopyresampled($image_drag, $image, $dst_x, $dst_y, 0, 0, $drag_w, $drag_h, $org_width, $org_height);
		
		
		
		//缩放到目标图里
		$image_dst = imagecreatetruecolor($dst_w, $dst_h);  //创建一个新的目标图像
		imagealphablending($image_dst,false);  //这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
		imagesavealpha($image_dst,true);  //这里很重要,意思是不要丢了$thumb图像的透明色;
		
		$white=imagecolorallocate($image_dst,255,255,255); 
		$black=imagecolorallocate($image_dst,0,0,0); 
		$red=imagecolorallocate($image_dst,255,0,0); 
		imagefill($image_dst,0,0,$white); 
		
		
		//复制图像的部分区块并调整大小
		//参数含义：$dst_image , $src_image , $dst_x , $dst_y , $src_x , $src_y , $dst_w , $dst_h , $src_w , $src_h 以左上角为基点
		
		imagecopyresampled($image_dst, $image_drag, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
		
		imagejpeg($image_dst, $out_path, 100);
		//imagepng($image_dst, $out_path);
		
		//echo 'resize success';
		return true;
    }
    
    
    
    
    //上传头像特效图
    public function savefilter(){
		
        if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }
        
		$path = $game_id."_filter_".date('ymdHis')."_".rand(10,99).".png";
		$dest = BASE_UPLOAD_PATH.$path;
		
		
		if($_FILES){
			//上传方式：表单
			$input = key($_FILES);
			if(!move_uploaded_file($_FILES[$input]['tmp_name'],$dest)) {
				$data['game_id']=$game_id;
		        $this->jsonData(1,'失败');
		        exit;
			}
		}
		elseif(isset($GLOBALS['HTTP_RAW_POST_DATA'])){ 
			//上传方式：原始POST
			$f = fopen($dest,'w');
			fwrite($f,$GLOBALS['HTTP_RAW_POST_DATA']);
			fclose($f);
		}
		else{ 
			//上传方式：客户端提交
			$f = fopen($dest,'w');
			fwrite($f,file_get_contents('php://input'));
			fclose($f);
		}
		
		
		$UserMod = M('game');
        $sql=sprintf("UPDATE %s SET filterpic='".addslashes($path)."' where id='".addslashes($game_id)."' ", $UserMod->getTableName() );
        $result = $UserMod->execute($sql);
        
		$data['game_id']=$game_id;
		$data['pic_url']=BASE_URL."/public/web_pic/".$path;
        $this->jsonData(0,'成功',$data);
        
    }
    
	
    
	
    //做已分享的标记
    public function fenxiang(){

        if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }
		
        $UserMod = M('game');
        $sql=sprintf("UPDATE %s SET isfenxiang='1' where id='".addslashes($game_id)."' ", $UserMod->getTableName() );
        $result = $UserMod->execute($sql);

        if($result==1){
            $data['game_id']=$game_id;
            $this->jsonData(0,'成功',$data);
            exit;
        }
        else{
            $data['game_id']=$game_id;
            $this->jsonData(1,'失败',$data);
            exit;
        }

    }
	
	
	//验证是否分享过
    public function isfenxiang(){

        if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }
		
		$CityMod = M('game');
        $fenxiang = $CityMod->field('id,isfenxiang')->where(" id='".addslashes($game_id)."' " )->order('id desc')->limit('0,1')->select();
        if(isset($fenxiang[0]['isfenxiang']) && $fenxiang[0]['isfenxiang']==1){
        	$data['game_id']=$game_id;
        	$data['isfenxiang']=$fenxiang[0]['isfenxiang'];
            $this->jsonData(0,'成功',$data);
            exit;
        }
        else{
        	$data['game_id']=$game_id;
        	$data['isfenxiang']=0;
        	$this->jsonData(1,'失败',$data);
            exit;
        }
    }
    
    
    
    //用户提交个人手机等联系信息
    public function setuserinfo(){
		
        if(isset($_SESSION['game_id']) && $_SESSION['game_id']>0){
            $game_id=$_SESSION['game_id'];
        }
        else{
            $this->jsonData(1,'请先参与游戏');
            exit;
        }
		
		$mobile=isset($_REQUEST['mobile'])?$_REQUEST['mobile']:"";
		
        $UserMod = M('game');
        $sql=sprintf("UPDATE %s SET mobile='".addslashes($mobile)."' where id='".addslashes($game_id)."' ", $UserMod->getTableName() );
        $result = $UserMod->execute($sql);

        if($result==1){
            $data['game_id']=$game_id;
            $this->jsonData(0,'成功',$data);
            exit;
        }
        else{
            $data['game_id']=$game_id;
            $this->jsonData(1,'失败',$data);
            exit;
        }

    }
	
	
    
    
    
	
	//获取某个公众号下所有的粉丝用户列表  http://www.sagaci.com.cn/home/getfanslist_test  测试
	public function getfanslist_test() {
		
		set_time_limit(0);
		
	    
		
		
		$CityMod = M('weixin_setting');
        $get_fans_expire_time = $CityMod->field('value_s')->where(" key_s='get_fans_time' " )->select();
        $get_fans_expire_time = $get_fans_expire_time[0]['value_s'];
        
        $nowtime=time();
        $cut_time=$nowtime-(1*60*60);
        if($cut_time<$get_fans_expire_time && 1==2){
        	//不足1小时，无需执行
        	echo "a";exit;
			
        	$return['success']='不足1小时，无需执行';
		    echo json_encode($return);
		    exit;
        }
        
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".WX_APPID_LIANMESHA."&secret=".WX_APPSECRET_LIANMESHA."";
		$get_return = json_decode($this->httpGet($url),true);
		if( !isset($get_return['access_token']) ){
			//echo '获取access_token失败！';
			//exit;
			echo "b";exit;
			
			$return['success']='获取access_token失败！';
		    echo json_encode($return);
		    exit;
		}
		
		$fans_list=array();
		
		$k=0;
		do{
			
			
			if(isset($user_list['next_openid']) && $user_list['next_openid']!=""){
				$next_openid=$user_list['next_openid'];
				$add_where="&next_openid=".$next_openid;
			}
			else{
				$next_openid='';
				$add_where='';
			}
			
			
			/*
			$v['openid']="";
			$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$get_return['access_token']."&openid=oEy59uFMXayYUQD5RfvBuZnKVHbg".$v['openid']."";
			//echo $url;exit;
			$user_info = json_decode($this->httpGet($url),true);
			echo "<pre>";print_r($user_info);exit;
			*/
			
			
			$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$get_return['access_token'].$add_where."";
			
			//if(isset($user_list['next_openid']) && $user_list['next_openid']!=""){
				//echo $url;exit;
			//}
			
			$user_list = json_decode($this->httpGet($url),true);
			
			//echo "<pre>";print_r($user_list['data']['openid']);echo "</pre>";exit;
			
			//if(isset($next_openid) && $next_openid!=""){
				//echo "<pre>";print_r($user_list);echo "</pre>";exit;
			//}
			
			
			if(isset($user_list['data']['openid']) && !empty($user_list['data']['openid'])){
				$fans_list = array_merge($fans_list, $user_list['data']['openid']);
			}
			
			//echo "<pre>";print_r($fans_list);echo "</pre>";exit;
			
			//echo $user_list['next_openid'];exit;
			
			$k=$k+1;
		}while(isset($user_list['next_openid']) && $user_list['next_openid']!="");
		
		//echo count($fans_list);
		//echo "<pre>";print_r($fans_list);echo "</pre>";exit;
		
		
		
		if(isset($fans_list) && !empty($fans_list)){
			
			
			$UserMod = M('user');
            
            $sql=sprintf("UPDATE %s SET 
		        is_fans='0' ", $UserMod->getTableName() );
	        //echo $sql;exit;
	        $UserMod->execute($sql);
		    
		    
			foreach($fans_list as $k=>$v){
				
				
				sleep(3);
				
				//不同公众号对应相同微信用户的openid是不同的，所以无法拿openid去比对
				//拿订阅号的openid的用户的昵称，然后去比对昵称
				$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$get_return['access_token']."&openid=".$v."";
				//echo $url;echo "<br>";
				$user_info = json_decode($this->httpGet($url),true);
				
				if(isset($user_info['nickname']) && $user_info['nickname']!=""){
					
					$nickname=$user_info['nickname'];
					//echo $nickname;exit;
					//$nickname=1;
					
					$is_fans=1;
		        	
			        $sql=sprintf("UPDATE %s SET 
			         is_fans='".addslashes($is_fans)."' 
			        where nickname=".addslashes($nickname)." 
			        ", $UserMod->getTableName() );
			        //echo $sql;exit;
			        $UserMod->execute($sql);
				
				}
				
			}
		}
		
		echo "finish";
		exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;
		
		
		//echo "finish";
		//exit;
		$return['success']='success';
	    echo json_encode($return);
	    exit;
	    
	}
	
    
    
    
	//获取某个公众号下所有的粉丝用户列表  http://www.sagaci.com.cn/home/getfanslist_test  测试
	public function getfanslist_nickname() {
		
		set_time_limit(0);
		
	    /*
		$CityMod = M('weixin_setting');
        $get_fans_expire_time = $CityMod->field('value_s')->where(" key_s='get_fans_time' " )->select();
        $get_fans_expire_time = $get_fans_expire_time[0]['value_s'];
        
        $nowtime=time();
        $cut_time=$nowtime-(1*60*60);
        if($cut_time<$get_fans_expire_time && 1==2){
        	//不足1小时，无需执行
        	echo "a";exit;
			
        	$return['success']='不足1小时，无需执行';
		    echo json_encode($return);
		    exit;
        }
        */
        
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".WX_APPID_LIANMESHA."&secret=".WX_APPSECRET_LIANMESHA."";
		$get_return = json_decode($this->httpGet($url),true);
		if( !isset($get_return['access_token']) ){
			//echo '获取access_token失败！';
			//exit;
			//echo "b";exit;
			
			$return['success']='获取access_token失败！';
		    echo json_encode($return);
		    exit;
		}
		
		$fans_list=array();
		
		/*
		$k=0;
		do{
			
			
			if(isset($user_list['next_openid']) && $user_list['next_openid']!=""){
				$next_openid=$user_list['next_openid'];
				$add_where="&next_openid=".$next_openid;
			}
			else{
				$next_openid='';
				$add_where='';
			}
			
			
			
			
			$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$get_return['access_token'].$add_where."";
			
			//if(isset($user_list['next_openid']) && $user_list['next_openid']!=""){
				//echo $url;exit;
			//}
			
			$user_list = json_decode($this->httpGet($url),true);
			
			//echo "<pre>";print_r($user_list['data']['openid']);echo "</pre>";exit;
			
			//if(isset($next_openid) && $next_openid!=""){
				//echo "<pre>";print_r($user_list);echo "</pre>";exit;
			//}
			
			
			if(isset($user_list['data']['openid']) && !empty($user_list['data']['openid'])){
				$fans_list = array_merge($fans_list, $user_list['data']['openid']);
			}
			
			//echo "<pre>";print_r($fans_list);echo "</pre>";exit;
			
			//echo $user_list['next_openid'];exit;
			
			$k=$k+1;
		}while(isset($user_list['next_openid']) && $user_list['next_openid']!="");
		*/
		
		
		//echo count($fans_list);
		//echo "<pre>";print_r($fans_list);echo "</pre>";exit;
		
		
		
		$fanslistMod = M('fanslist');
		$andsql=" 1 ";
    	$fans_list = $fanslistMod->where(" 1 ".$andsql." ")->order('modify_time asc')->limit('0,10')->select();
    	
    	echo "<pre>";print_r($fans_list);echo "</pre>";exit;
    	
		    	
            $sql=" id > 0 ";
            $fanslistMod->where($sql)->delete();
            
		
		if(isset($fans_list) && !empty($fans_list)){
			
			
			$UserMod = M('user');
            
            $sql=sprintf("UPDATE %s SET 
		        is_fans='0' ", $UserMod->getTableName() );
	        //echo $sql;exit;
	        $UserMod->execute($sql);
		    
		    
			foreach($fans_list as $k=>$v){
				
				
				sleep(3);
				
				//不同公众号对应相同微信用户的openid是不同的，所以无法拿openid去比对
				//拿订阅号的openid的用户的昵称，然后去比对昵称
				$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$get_return['access_token']."&openid=".$v."";
				//echo $url;echo "<br>";
				$user_info = json_decode($this->httpGet($url),true);
				
				if(isset($user_info['nickname']) && $user_info['nickname']!=""){
					
					$nickname=$user_info['nickname'];
					//echo $nickname;exit;
					//$nickname=1;
					
					$is_fans=1;
		        	
			        $sql=sprintf("UPDATE %s SET 
			         is_fans='".addslashes($is_fans)."' 
			        where nickname=".addslashes($nickname)." 
			        ", $UserMod->getTableName() );
			        //echo $sql;exit;
			        $UserMod->execute($sql);
				
				}
				
			}
		}
		
		echo "finish";
		exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;exit;
		
		
		//echo "finish";
		//exit;
		$return['success']='success';
	    echo json_encode($return);
	    exit;
	    
	}
	
    
    

}
?>